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 ¶
AddHeaderMap to reqeust instance.
func AddHeaders ¶
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 ¶
AppendQueryToURL appends the given query string to the given url.
func AppendQueryToURLString ¶
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 ¶
HeaderToString convert http Header to string
func HeaderToStringMap ¶
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 ¶
MergeURLValues merge url.Values by overwrite.
values support: url.Values, map[string]string, map[string][]string
func ParseAccept ¶
ParseAccept header to strings. referred from gin framework
eg: acceptHeader = "application/json, text/plain, */*"
func RequestToString ¶
RequestToString convert http Request to string
func ResponseToString ¶
ResponseToString convert http Response to string
func SendRequest ¶
SendRequest quick send a request by default client
func SetHeaderMap ¶
SetHeaderMap to reqeust instance.
func SetHeaders ¶
SetHeaders sets the key, value pairs from the given http.Header to the request. Values for existing keys are overwritten.
func SetTimeout ¶
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 ¶
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)
Types ¶
type BasicAuthConf ¶
BasicAuthConf struct
func (*BasicAuthConf) String ¶
func (ba *BasicAuthConf) String() string
String build to auth header "Authorization".
func (*BasicAuthConf) Value ¶
func (ba *BasicAuthConf) Value() string
Value build to auth header "Authorization".
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client a simple http request client.
func NewWithTimeout ¶
NewWithTimeout new instance use http.Client and with custom timeout(ms)
func (*Client) AnyBody ¶
AnyBody with custom body.
Allow type:
- string, []byte, map[string][]string/url.Values, io.Reader(eg: bytes.Buffer, strings.Reader)
func (*Client) ContentType ¶
ContentType set default content-Type header.
func (*Client) DefaultHeader ¶
DefaultHeader set default header for all requests
func (*Client) DefaultHeaderMap ¶
DefaultHeaderMap set default headers for all requests
func (*Client) DefaultMethod ¶
DefaultMethod set default request method
func (*Client) JSONBytesBody ¶
JSONBytesBody with custom bytes body, and set JSON content type
func (*Client) OnAfterSend ¶
func (h *Client) OnAfterSend(fn AfterSendFn) *Client
OnAfterSend add callback after send.
func (*Client) OnBeforeSend ¶
OnBeforeSend add callback before send.
func (*Client) SendRequest ¶
SendRequest send request and return http response
func (*Client) SendWithOpt ¶
SendWithOpt request and return http response
func (*Client) SetTimeout ¶
SetTimeout set default timeout for http client doer
func (*Client) StringBody ¶
StringBody with custom string body
func (*Client) WithOption ¶
WithOption with custom request options
type Option ¶
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 ¶
AnyBody with custom body.
Allow type:
- string, []byte, map[string][]string/url.Values, io.Reader(eg: bytes.Buffer, strings.Reader)
func (*Option) JSONBytesBody ¶
JSONBytesBody with custom bytes body, and set JSON content type
func (*Option) StringBody ¶
StringBody with custom string body
func (*Option) WithContentType ¶
WithContentType set content type
func (*Option) WithHeader ¶
WithHeader set header
func (*Option) WithHeaderMap ¶
WithHeaderMap set header map
func (*Option) WithMethod ¶
WithMethod set method
func (*Option) WithOptionFn ¶
WithOptionFn set option func
func (*Option) WithOptionFns ¶
WithOptionFns set option func
type RespX ¶
RespX wrap http.Response and add some useful methods.
func (*RespX) BindJSON ¶
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 ¶
BindJSONOnOk body data on response status is 200. if ptr is nil, will discard body data.
NOTICE: must close resp body.
func (*RespX) ContentType ¶
ContentType get response content type
func (*RespX) IsEmptyBody ¶
IsEmptyBody check response body is empty
func (*RespX) SafeCloseBody ¶
func (r *RespX) SafeCloseBody()
SafeCloseBody close resp body, ignore error