httpgo

package module
v0.0.0-...-2b581f7 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 9, 2023 License: Apache-2.0 Imports: 16 Imported by: 0

README

httpgo

A fast and simple go http client

⚙️ Installation

go get -u github.com/voocel/httpgo

👀 Examples

📖GET
res, err := httpgo.Get("http://www.google.com").Do()
📖POST
res, err := httpgo.Post("http://www.google.com").Do()
📖SetTimeout
res, err := httpgo.Get("http://www.google.com").SetTimeout(5 * time.Second).Do()
📖Middleware
var l *log.logger
c := httpgo.NewHttpClient(WithLogger(l))

res, err := c.Get("http://www.google.com").Do()
res, err := c.Post("http://www.google.com").Do()

🔥 Supported Methods

  • SetQuery(key, value string)
  • SetQueries(m map[string]string)
  • SetForm(m map[string]string)
  • SetJSON(v string)
  • SetText(v string)
  • SetFile(fieldname, filename string)
  • SetTimeout(t time.Duration)
  • SetHeader(key, value string)
  • SetHeaders(m map[string]string)
  • AddHeader(key, value string)
  • AddHeaders(m map[string]string)
  • SetUA(ua string)
  • AddCookie(c *http.Cookie)
  • BasicAuth(username, password string)
  • Upload(fieldname, filename string)

Documentation

Index

Constants

View Source
const (
	GET     = "GET"
	POST    = "POST"
	PUT     = "PUT"
	HEAD    = "HEAD"
	PATCH   = "PATCH"
	DELETE  = "DELETE"
	OPTIONS = "OPTIONS"
)

HTTP methods support

View Source
const (
	JSON            = "application/json;charset=UTF-8"
	TEXT            = "text/plain;charset=UTF-8"
	FORM_DATA       = "multipart/form-data;charset=UTF-8"
	FORM_URLENCODED = "application/x-www-form-urlencoded;charset=UTF-8"
)

content type support

Variables

View Source
var DefaultClient = NewClient()

Functions

func AsyncGet

func AsyncGet(rawUrl string, ch chan<- *AsyncResponse)

func Middleware

func Middleware(t http.RoundTripper, mfs ...MiddlewareFunc) http.RoundTripper

Types

type AsyncResponse

type AsyncResponse struct {
	Resp *Response
	Err  error
}

type Client

type Client struct {
	*http.Client
	// contains filtered or unexported fields
}

func NewClient

func NewClient(mfs ...MiddlewareFunc) *Client

NewClient create a client

type Handle

type Handle func(*Request) (*Response, error)

type HttpClient

type HttpClient struct {
	// contains filtered or unexported fields
}

func NewHttpClient

func NewHttpClient(mfs ...MiddlewareFunc) *HttpClient

func (*HttpClient) Get

func (h *HttpClient) Get(rawUrl string) *Request

func (*HttpClient) NewRequest

func (h *HttpClient) NewRequest(method, rawUrl string) *Request

func (*HttpClient) Post

func (h *HttpClient) Post(rawUrl string) *Request

type MiddlewareFunc

type MiddlewareFunc func(http.RoundTripper) http.RoundTripper

func WithBasicAuth

func WithBasicAuth(username, password string) MiddlewareFunc

func WithLogger

func WithLogger(l *log.Logger) MiddlewareFunc

type Request

type Request struct {
	*http.Request
	Err error
	// contains filtered or unexported fields
}

func Delete

func Delete(rawUrl string) *Request

Delete http request

func Get

func Get(rawUrl string) *Request

Get http request

func Head(rawUrl string) *Request

Head http request

func NewRequest

func NewRequest(method, rawUrl string) *Request

NewRequest create request

func Options

func Options(rawUrl string) *Request

Options http request

func Patch

func Patch(rawUrl string) *Request

Patch http request

func Post

func Post(rawUrl string) *Request

Post http request

func Put

func Put(rawUrl string) *Request

Put http request

func (*Request) AddCookie

func (r *Request) AddCookie(c *http.Cookie) *Request

AddCookie add cookie for the request

func (*Request) AddHeader

func (r *Request) AddHeader(key, value string) *Request

AddHeader add a header for the request

func (*Request) AddHeaders

func (r *Request) AddHeaders(m map[string]string) *Request

AddHeaders add headers from a map for the request

func (*Request) BasicAuth

func (r *Request) BasicAuth(username, password string) *Request

BasicAuth make basic authentication

func (*Request) Do

func (r *Request) Do() (resp *Response, err error)

Do finish do

func (*Request) SetFile

func (r *Request) SetFile(field, filename string) *Request

SetFile set the file

func (*Request) SetForm

func (r *Request) SetForm(m map[string]string) *Request

SetForm set the form data from a map

func (*Request) SetHeader

func (r *Request) SetHeader(key, value string) *Request

SetHeader set a header for the request

func (*Request) SetHeaders

func (r *Request) SetHeaders(m map[string]string) *Request

SetHeaders set headers from a map for the request

func (*Request) SetJSON

func (r *Request) SetJSON(v string) *Request

SetJSON set the json data

func (*Request) SetQueries

func (r *Request) SetQueries(m map[string]string) *Request

SetQueries set URL query params for the request

func (*Request) SetQuery

func (r *Request) SetQuery(key, value string) *Request

SetQuery set an URL query parameter for the request

func (*Request) SetText

func (r *Request) SetText(v string) *Request

SetText set the text data

func (*Request) SetTimeout

func (r *Request) SetTimeout(t time.Duration) *Request

SetTimeout set the request timeout

func (*Request) SetUA

func (r *Request) SetUA(ua string) *Request

SetUA set user-agent for the request

type Response

type Response struct {
	*http.Response
	Req  *Request
	Body []byte
}

func (*Response) GetBody

func (r *Response) GetBody() []byte

GetBody get response body

func (*Response) GetStatusCode

func (r *Response) GetStatusCode() int

GetStatusCode get HTTP status code

type TransportFunc

type TransportFunc func(*http.Request) (*http.Response, error)

func (TransportFunc) RoundTrip

func (tf TransportFunc) RoundTrip(r *http.Request) (*http.Response, error)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL