Documentation
¶
Overview ¶
Package httpreq an simple http requester
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 ConfigStd(fn func(hc *http.Client))
- func Get(url string, opt *ReqOption) (*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 Post(url string, data any, opt *ReqOption) (*http.Response, error)
- func RequestToString(r *http.Request) string
- func ResponseToString(w *http.Response) string
- func ToQueryValues(data any) url.Values
- func ToRequestBody(data any) io.Reader
- type BasicAuthConf
- type Doer
- type DoerFunc
- type Option
- type Reqdeprecated
- type ReqClient
- func (h *ReqClient) AnyBody(data any) *ReqClient
- func (h *ReqClient) BaseURL(baseURL string) *ReqClient
- func (h *ReqClient) BytesBody(bs []byte) *ReqClient
- func (h *ReqClient) Client(c Doer) *ReqClient
- func (h *ReqClient) ContentType(cType string) *ReqClient
- func (h *ReqClient) Doer() Doer
- func (h *ReqClient) JSONBytesBody(bs []byte) *ReqClient
- func (h *ReqClient) Method(method string) *ReqClient
- func (h *ReqClient) MustSend(url string) *http.Response
- func (h *ReqClient) OnAfterSend(fn func(resp *http.Response)) *ReqClient
- func (h *ReqClient) OnBeforeSend(fn func(req *http.Request)) *ReqClient
- func (h *ReqClient) Send(url string) (*http.Response, error)
- func (h *ReqClient) SendRequest(req *http.Request, opt *ReqOption) (*http.Response, error)
- func (h *ReqClient) SendWithOpt(url string, opt *ReqOption) (*http.Response, error)
- func (h *ReqClient) StringBody(s string) *ReqClient
- func (h *ReqClient) WithBody(r io.Reader) *ReqClient
- func (h *ReqClient) WithHeader(key, val string) *ReqClient
- func (h *ReqClient) WithHeaders(kvMap map[string]string) *ReqClient
- type ReqLogger
- type ReqOption
- type Resp
- func (r *Resp) BindJSON(ptr any) error
- func (r *Resp) BindJSONOnOk(ptr any) error
- func (r *Resp) BodyBuffer() *bytes.Buffer
- func (r *Resp) BodyString() string
- func (r *Resp) CloseBody() error
- func (r *Resp) ContentType() string
- func (r *Resp) IsEmptyBody() bool
- func (r *Resp) IsFail() bool
- func (r *Resp) IsOk() bool
- func (r *Resp) IsSuccessful() bool
- func (r *Resp) QuiteCloseBody()
- func (r *Resp) 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 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 ToQueryValues ¶
ToQueryValues convert string-map or any-map to url.Values
func ToRequestBody ¶ added in v0.6.8
ToRequestBody convert handle
Allow type for data:
- string
- []byte
- map[string]string
- map[string][]string/url.Values
- io.Reader(eg: bytes.Buffer, strings.Reader)
Types ¶
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 Option ¶ added in v0.6.9
type Option struct {
// Method for request
Method string
// HeaderMap data. eg: traceid
HeaderMap map[string]string
// Timeout unit: ms
Timeout int
// TCancelFunc will auto set it on Timeout > 0
TCancelFunc context.CancelFunc
// ContentType header
ContentType string
// EncodeJSON req body
EncodeJSON bool
// Logger for request
Logger ReqLogger
// Context for request
Context context.Context
}
Option struct
type ReqClient ¶ added in v0.6.0
type ReqClient struct {
// contains filtered or unexported fields
}
ReqClient an simple http request client.
func (*ReqClient) AnyBody ¶ added in v0.6.0
AnyBody with custom body.
Allow type:
- string, []byte, map[string][]string/url.Values, io.Reader(eg: bytes.Buffer, strings.Reader)
func (*ReqClient) ContentType ¶ added in v0.6.0
ContentType with custom content-Type header.
func (*ReqClient) JSONBytesBody ¶ added in v0.6.0
JSONBytesBody with custom bytes body, and set JSON content type
func (*ReqClient) OnAfterSend ¶ added in v0.6.8
OnAfterSend add callback after send.
func (*ReqClient) OnBeforeSend ¶ added in v0.6.8
OnBeforeSend add callback before send.
func (*ReqClient) SendRequest ¶ added in v0.6.8
SendRequest request and return http response
func (*ReqClient) SendWithOpt ¶ added in v0.6.0
SendWithOpt request and return http response
func (*ReqClient) StringBody ¶ added in v0.6.0
StringBody with custom string body
func (*ReqClient) WithHeader ¶ added in v0.6.0
WithHeader with custom header
type Resp ¶ added in v0.6.0
Resp struct
func (*Resp) BindJSONOnOk ¶ added in v0.6.1
BindJSONOnOk body data on status is 200
NOTICE: must close resp body.
func (*Resp) BodyBuffer ¶ added in v0.6.0
BodyBuffer read body to buffer.
NOTICE: must close resp body.
func (*Resp) BodyString ¶ added in v0.6.0
BodyString get body as string.
func (*Resp) ContentType ¶ added in v0.6.0
ContentType get response content type
func (*Resp) IsEmptyBody ¶ added in v0.6.0
IsEmptyBody check response body is empty
func (*Resp) QuiteCloseBody ¶ added in v0.6.0
func (r *Resp) QuiteCloseBody()
QuiteCloseBody close resp body, ignore error