Documentation
¶
Overview ¶
Package httpreq an simple http requester
Index ¶
- func AddHeaders(req *http.Request, header http.Header)
- func BuildBasicAuth(username, password string) string
- func ConfigStd(fn func(hc *http.Client))
- func Get(url string, opt *ReqOption) (*http.Response, error)
- func HeaderToStringMap(rh http.Header) map[string]string
- func IsClientError(statusCode int) bool
- func IsForbidden(statusCode int) 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
- type Doer
- type DoerFunc
- type Reqdeprecated
- type ReqClient
- func (h *ReqClient) AfterSend(fn func(resp *http.Response)) *ReqClient
- func (h *ReqClient) AnyBody(data any) *ReqClient
- func (h *ReqClient) BaseURL(baseURL string) *ReqClient
- func (h *ReqClient) BeforeSend(fn func(req *http.Request)) *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) Send(url string) (*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 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 BuildBasicAuth ¶
BuildBasicAuth returns the base64 encoded username:password for basic auth. copied from net/http.
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 to url.Values
Types ¶
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) BeforeSend ¶ added in v0.6.0
BeforeSend add callback before send.
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) 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 ReqOption ¶ added in v0.6.0
type ReqOption struct {
// HeaderMap data. eg: traceid, parentid
HeaderMap map[string]string
// Timeout unit: ms
Timeout int
// ContentType header
ContentType string
// EncodeJSON req body
EncodeJSON bool
// Logger for request
Logger ReqLogger
}
ReqOption struct
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