Documentation ¶
Overview ¶
Package httpreq provide an simple http requester and some useful util functions.
Index ¶
- func AddHeaderMap(req *http.Request, headerMap map[string]string)
- func AddHeaders(req *http.Request, header http.Header)
- func AppendQueryToURL(reqURL *url.URL, uv url.Values) error
- func AppendQueryToURLString(urlStr string, query url.Values) string
- func BuildBasicAuth(username, password string) string
- func Config(fn func(hc *http.Client))
- func Delete(url string, optFns ...OptionFn) (*http.Response, error)
- func Get(url string, optFns ...OptionFn) (*http.Response, error)
- func HeaderToString(h http.Header) string
- func HeaderToStringMap(rh http.Header) map[string]string
- func IsClientError(statusCode int) bool
- func IsForbidden(statusCode int) bool
- func IsNoBodyMethod(method string) bool
- func IsNotFound(statusCode int) bool
- func IsOK(statusCode int) bool
- func IsRedirect(statusCode int) bool
- func IsServerError(statusCode int) bool
- func IsSuccessful(statusCode int) bool
- func MakeBody(data any, cType string) io.Reader
- func MakeQuery(data any) url.Values
- func MergeURLValues(uv url.Values, values ...any) url.Values
- func MustResp(r *http.Response, err error) *http.Response
- func MustSend(method, url string, optFns ...OptionFn) *http.Response
- func ParseAccept(acceptHeader string) []string
- func Post(url string, data any, optFns ...OptionFn) (*http.Response, error)
- func PostJSON(url string, data any, optFns ...OptionFn) (*http.Response, error)
- func Put(url string, data any, optFns ...OptionFn) (*http.Response, error)
- func RequestToString(r *http.Request) string
- func ResponseToString(w *http.Response) string
- func Send(method, url string, optFns ...OptionFn) (*http.Response, error)
- func SendRequest(req *http.Request, opt *Option) (*http.Response, error)
- func SetHeaderMap(req *http.Request, headerMap map[string]string)
- func SetHeaders(req *http.Request, headers ...http.Header)
- func SetTimeout(ms int)
- func ToQueryValues(data any) url.Values
- func ToRequestBody(data any, cType string) io.Reader
- func WithJSONType(opt *Option)
- type AfterSendFn
- type BasicAuthConf
- type Client
- func (h *Client) AnyBody(data any) *Option
- func (h *Client) BaseURL(baseURL string) *Client
- func (h *Client) BytesBody(bs []byte) *Option
- func (h *Client) ContentType(cType string) *Client
- func (h *Client) DefaultHeader(key, val string) *Client
- func (h *Client) DefaultHeaderMap(kvMap map[string]string) *Client
- func (h *Client) DefaultMethod(method string) *Client
- func (h *Client) Delete(url string, optFns ...OptionFn) (*http.Response, error)
- func (h *Client) Doer() Doer
- func (h *Client) FormBody(data any) *Option
- func (h *Client) Get(url string, optFns ...OptionFn) (*http.Response, error)
- func (h *Client) JSONBody(data any) *Option
- func (h *Client) JSONBytesBody(bs []byte) *Option
- func (h *Client) MustSend(method, url string, optFns ...OptionFn) *http.Response
- func (h *Client) OnAfterSend(fn AfterSendFn) *Client
- func (h *Client) OnBeforeSend(fn func(req *http.Request)) *Client
- func (h *Client) Post(url string, data any, optFns ...OptionFn) (*http.Response, error)
- func (h *Client) PostJSON(url string, data any, optFns ...OptionFn) (*http.Response, error)
- func (h *Client) Put(url string, data any, optFns ...OptionFn) (*http.Response, error)
- func (h *Client) Send(method, url string, optFns ...OptionFn) (*http.Response, error)
- func (h *Client) SendRequest(req *http.Request, opt *Option) (*http.Response, error)
- func (h *Client) SendWithOpt(url string, opt *Option) (*http.Response, error)
- func (h *Client) SetClient(c Doer) *Client
- func (h *Client) SetTimeout(ms int) *Client
- func (h *Client) StringBody(s string) *Option
- func (h *Client) WithBody(r io.Reader) *Option
- func (h *Client) WithData(data any) *Option
- func (h *Client) WithOption(optFns ...OptionFn) *Option
- type Doer
- type DoerFunc
- type Option
- func (o *Option) AnyBody(data any) *Option
- func (o *Option) BytesBody(bs []byte) *Option
- func (o *Option) Copy() *Option
- func (o *Option) Delete(url string, fns ...OptionFn) (*http.Response, error)
- func (o *Option) FormBody(data any) *Option
- func (o *Option) Get(url string, fns ...OptionFn) (*http.Response, error)
- func (o *Option) JSONBytesBody(bs []byte) *Option
- func (o *Option) MustSend(method, url string, fns ...OptionFn) *http.Response
- func (o *Option) Post(url string, data any, fns ...OptionFn) (*http.Response, error)
- func (o *Option) Put(url string, data any, fns ...OptionFn) (*http.Response, error)
- func (o *Option) Send(method, url string, fns ...OptionFn) (*http.Response, error)
- func (o *Option) StringBody(s string) *Option
- func (o *Option) WithBody(r io.Reader) *Option
- func (o *Option) WithClient(cli *Client) *Option
- func (o *Option) WithContentType(ct string) *Option
- func (o *Option) WithData(data any) *Option
- func (o *Option) WithHeader(key, val string) *Option
- func (o *Option) WithHeaderMap(m map[string]string) *Option
- func (o *Option) WithJSON(data any) *Option
- func (o *Option) WithMethod(method string) *Option
- func (o *Option) WithOptionFn(fns ...OptionFn) *Option
- func (o *Option) WithOptionFns(fns []OptionFn) *Option
- type OptionFn
- type Options
- type ReqLogger
- type Resp
- type RespX
- func (r *RespX) BindJSON(ptr any) error
- func (r *RespX) BindJSONOnOk(ptr any) error
- func (r *RespX) BodyBuffer() *bytes.Buffer
- func (r *RespX) BodyString() string
- func (r *RespX) CloseBody() error
- func (r *RespX) ContentType() string
- func (r *RespX) IsEmptyBody() bool
- func (r *RespX) IsFail() bool
- func (r *RespX) IsOk() bool
- func (r *RespX) IsSuccessful() bool
- func (r *RespX) SafeCloseBody()
- func (r *RespX) String() string
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AddHeaderMap ¶ added in v0.6.8
AddHeaderMap to reqeust instance.
func AddHeaders ¶ added in v0.5.12
AddHeaders adds the key, value pairs from the given http.Header to the request. Values for existing keys are appended to the keys values.
func AppendQueryToURL ¶ added in v0.6.8
AppendQueryToURL appends the given query string to the given url.
func AppendQueryToURLString ¶ added in v0.6.8
AppendQueryToURLString appends the given query data to the given url.
func BuildBasicAuth ¶
BuildBasicAuth returns the base64 encoded username:password for basic auth. Then set to header "Authorization".
copied from net/http.
func HeaderToString ¶ added in v0.6.8
HeaderToString convert http Header to string
func HeaderToStringMap ¶ added in v0.6.0
HeaderToStringMap convert
func IsClientError ¶
IsClientError check response is client error (400 - 500)
func IsForbidden ¶
IsForbidden is this response forbidden(403)
func IsRedirect ¶
IsRedirect check response status code is in [301, 302, 303, 307]
func IsServerError ¶
IsServerError check response is server error (500 - 600)
func IsSuccessful ¶
IsSuccessful check response status code is in 200 - 300
func MergeURLValues ¶ added in v0.6.11
MergeURLValues merge url.Values by overwrite.
values support: url.Values, map[string]string, map[string][]string
func ParseAccept ¶ added in v0.6.11
ParseAccept header to strings. referred from gin framework
func PostJSON ¶ added in v0.6.11
PostJSON quick send a POST request by default client, with JSON content type
func RequestToString ¶ added in v0.4.5
RequestToString convert http Request to string
func ResponseToString ¶ added in v0.4.5
ResponseToString convert http Response to string
func SendRequest ¶ added in v0.6.10
SendRequest quick send a request by default client
func SetHeaderMap ¶ added in v0.6.11
SetHeaderMap to reqeust instance.
func SetHeaders ¶ added in v0.6.11
SetHeaders sets the key, value pairs from the given http.Header to the request. Values for existing keys are overwritten.
func SetTimeout ¶ added in v0.6.10
func SetTimeout(ms int)
SetTimeout set default timeout(ms) for std client
Note: timeout unit is millisecond
func ToQueryValues ¶
ToQueryValues convert string-map or any-map to url.Values
data support:
- url.Values
- []byte
- string
- map[string][]string
- map[string]string
- map[string]any
func ToRequestBody ¶ added in v0.6.8
ToRequestBody make request body, convert data to io.Reader
Allow type for data:
- string
- []byte
- map[string]string
- map[string][]string/url.Values
- io.Reader(eg: bytes.Buffer, strings.Reader)
func WithJSONType ¶ added in v0.6.10
func WithJSONType(opt *Option)
WithJSONType set request content type to JSON
Types ¶
type AfterSendFn ¶ added in v0.6.10
AfterSendFn callback func
type BasicAuthConf ¶ added in v0.6.8
BasicAuthConf struct
func (*BasicAuthConf) IsValid ¶ added in v0.6.8
func (ba *BasicAuthConf) IsValid() bool
IsValid value
func (*BasicAuthConf) String ¶ added in v0.6.8
func (ba *BasicAuthConf) String() string
String build to auth header "Authorization".
func (*BasicAuthConf) Value ¶ added in v0.6.8
func (ba *BasicAuthConf) Value() string
Value build to auth header "Authorization".
type Client ¶ added in v0.6.10
type Client struct {
// contains filtered or unexported fields
}
Client a simple http request client.
func NewClient ¶ added in v0.6.9
NewClient create a new http client and cache it.
Note: timeout unit is millisecond
func NewWithDoer ¶ added in v0.6.10
NewWithDoer instance with custom http client
func NewWithTimeout ¶ added in v0.6.11
NewWithTimeout new instance use http.Client and with custom timeout(ms)
func (*Client) AnyBody ¶ added in v0.6.10
AnyBody with custom body.
Allow type:
- string, []byte, map[string][]string/url.Values, io.Reader(eg: bytes.Buffer, strings.Reader)
func (*Client) ContentType ¶ added in v0.6.10
ContentType set default content-Type header.
func (*Client) DefaultHeader ¶ added in v0.6.10
DefaultHeader set default header for all requests
func (*Client) DefaultHeaderMap ¶ added in v0.6.10
DefaultHeaderMap set default headers for all requests
func (*Client) DefaultMethod ¶ added in v0.6.10
DefaultMethod set default request method
func (*Client) Delete ¶ added in v0.6.10
Delete send DELETE request with options, return http response
func (*Client) JSONBytesBody ¶ added in v0.6.10
JSONBytesBody with custom bytes body, and set JSON content type
func (*Client) OnAfterSend ¶ added in v0.6.10
func (h *Client) OnAfterSend(fn AfterSendFn) *Client
OnAfterSend add callback after send.
func (*Client) OnBeforeSend ¶ added in v0.6.10
OnBeforeSend add callback before send.
func (*Client) PostJSON ¶ added in v0.6.11
PostJSON send JSON POST request with options, return http response
func (*Client) SendRequest ¶ added in v0.6.10
SendRequest send request and return http response
func (*Client) SendWithOpt ¶ added in v0.6.10
SendWithOpt request and return http response
func (*Client) SetTimeout ¶ added in v0.6.11
SetTimeout set default timeout for http client doer
func (*Client) StringBody ¶ added in v0.6.10
StringBody with custom string body
func (*Client) WithOption ¶ added in v0.6.10
WithOption with custom request options
type Option ¶ added in v0.6.9
type Option struct { // Timeout for request. unit: ms Timeout int // Method for request Method string // HeaderMap data. eg: traceid HeaderMap map[string]string // ContentType header ContentType string // Logger for request Logger ReqLogger // Context for request Context context.Context // Data for request. can be used on any request method. // // type allow: // string, []byte, io.Reader, map[string]string, ... Data any // Body data for request. used on POST, PUT, PATCH method. // // eg: strings.NewReader("name=inhere") Body io.Reader // contains filtered or unexported fields }
Option struct
func (*Option) AnyBody ¶ added in v0.6.10
AnyBody with custom body.
Allow type:
- string, []byte, map[string][]string/url.Values, io.Reader(eg: bytes.Buffer, strings.Reader)
func (*Option) JSONBytesBody ¶ added in v0.6.10
JSONBytesBody with custom bytes body, and set JSON content type
func (*Option) StringBody ¶ added in v0.6.10
StringBody with custom string body
func (*Option) WithClient ¶ added in v0.6.10
WithClient set client
func (*Option) WithContentType ¶ added in v0.6.10
WithContentType set content type
func (*Option) WithHeader ¶ added in v0.6.10
WithHeader set header
func (*Option) WithHeaderMap ¶ added in v0.6.10
WithHeaderMap set header map
func (*Option) WithMethod ¶ added in v0.6.10
WithMethod set method
func (*Option) WithOptionFn ¶ added in v0.6.10
WithOptionFn set option func
func (*Option) WithOptionFns ¶ added in v0.6.10
WithOptionFns set option func
type RespX ¶ added in v0.6.10
RespX wrap http.Response and add some useful methods.
func (*RespX) BindJSON ¶ added in v0.6.10
BindJSON body data to a ptr, will don't check status code. if ptr is nil, will discard body data.
NOTICE: must close resp body.
func (*RespX) BindJSONOnOk ¶ added in v0.6.10
BindJSONOnOk body data on response status is 200. if ptr is nil, will discard body data.
NOTICE: must close resp body.
func (*RespX) BodyBuffer ¶ added in v0.6.10
BodyBuffer read body to buffer.
NOTICE: must close resp body.
func (*RespX) BodyString ¶ added in v0.6.10
BodyString get body as string.
func (*RespX) ContentType ¶ added in v0.6.10
ContentType get response content type
func (*RespX) IsEmptyBody ¶ added in v0.6.10
IsEmptyBody check response body is empty
func (*RespX) IsSuccessful ¶ added in v0.6.10
IsSuccessful check
func (*RespX) SafeCloseBody ¶ added in v0.6.10
func (r *RespX) SafeCloseBody()
SafeCloseBody close resp body, ignore error