Documentation
¶
Overview ¶
Package hreq is a simple http client request builder, inspired from https://github.com/dghubble/sling
Index ¶
- func ToString(r *http.Request, err error) string
- type BodyProvider
- type HReq
- func (h *HReq) AddHeader(key, value string) *HReq
- func (h *HReq) AddHeaders(headers http.Header) *HReq
- func (h *HReq) BaseURL(baseURL string) *HReq
- func (h *HReq) BasicAuth(username, password string) *HReq
- func (h *HReq) Body(body interface{}) *HReq
- func (h *HReq) BodyProvider(bp BodyProvider) *HReq
- func (h *HReq) BodyReader(r io.Reader) *HReq
- func (h *HReq) Build(pathURLAndMethod ...string) (*http.Request, error)
- func (h *HReq) BytesBody(bs []byte) *HReq
- func (h *HReq) Client(doer httpreq.Doer) *HReq
- func (h *HReq) Config(fn func(doer httpreq.Doer)) *HReq
- func (h *HReq) ConfigHClient(fn func(hClient *http.Client)) *HReq
- func (h *HReq) Connect(pathURL string) (*Response, error)
- func (h *HReq) ConnectDo(pathURL string) (*Response, error)
- func (h *HReq) ContentType(value string) *HReq
- func (h *HReq) Delete(pathURL string) *HReq
- func (h *HReq) DeleteDo(pathURL string) (*Response, error)
- func (h *HReq) Do(pathURLAndMethod ...string) (*Response, error)
- func (h *HReq) DoWithCtx(ctx context.Context, pathURLAndMethod ...string) (*Response, error)
- func (h *HReq) Doer(doer httpreq.Doer) *HReq
- func (h *HReq) FileContentsBody(filepath string) *HReq
- func (h *HReq) FormBody(formData interface{}) *HReq
- func (h *HReq) FormType() *HReq
- func (h *HReq) Get(pathURL string) *HReq
- func (h *HReq) GetDo(pathURL string) (*Response, error)
- func (h *HReq) Head(pathURL string) *HReq
- func (h *HReq) HeadDo(pathURL string) (*Response, error)
- func (h *HReq) HttpClient(hClient *http.Client) *HReq
- func (h *HReq) JSONBody(jsonData interface{}) *HReq
- func (h *HReq) JSONType() *HReq
- func (h *HReq) Method(method string) *HReq
- func (h *HReq) Middleware(middles ...Middleware) *HReq
- func (h *HReq) Middlewares(middles ...Middleware) *HReq
- func (h *HReq) Multipart(key, value string) *HReq
- func (h *HReq) MultipartType() *HReq
- func (h *HReq) MustSend(pathURLAndMethod ...string) *Response
- func (h *HReq) New() *HReq
- func (h *HReq) NewRequest(pathURLAndMethod ...string) (*http.Request, error)
- func (h *HReq) NewRequestWithCtx(ctx context.Context, pathURLAndMethod ...string) (*http.Request, error)
- func (h *HReq) Options(pathURL string) *HReq
- func (h *HReq) OptionsDo(pathURL string) (*Response, error)
- func (h *HReq) Patch(pathURL string) *HReq
- func (h *HReq) PatchDo(pathURL string) (*Response, error)
- func (h *HReq) PathURL(pathURL string) *HReq
- func (h *HReq) Post(pathURL string) *HReq
- func (h *HReq) PostDo(pathURL string) (*Response, error)
- func (h *HReq) Put(pathURL string) *HReq
- func (h *HReq) PutDo(pathURL string) (*Response, error)
- func (h *HReq) QueryParam(key string, value interface{}) *HReq
- func (h *HReq) QueryParams(ps interface{}) *HReq
- func (h *HReq) QueryValues(values url.Values) *HReq
- func (h *HReq) Send(pathURLAndMethod ...string) (*Response, error)
- func (h *HReq) SendRequest(req *http.Request) (*Response, error)
- func (h *HReq) SendWithCtx(ctx context.Context, pathURLAndMethod ...string) (*Response, error)
- func (h *HReq) SetCookieString(value string) *HReq
- func (h *HReq) SetCookies(hcs ...*http.Cookie) *HReq
- func (h *HReq) SetHeader(key, value string) *HReq
- func (h *HReq) SetHeaders(headers http.Header) *HReq
- func (h *HReq) String() string
- func (h *HReq) StringBody(s string) *HReq
- func (h *HReq) Trace(pathURL string) *HReq
- func (h *HReq) TraceDo(pathURL string) (*Response, error)
- func (h *HReq) Use(middles ...Middleware) *HReq
- func (h *HReq) UserAgent(value string) *HReq
- func (h *HReq) UserAuth(value string) *HReq
- func (h *HReq) Uses(middles ...Middleware) *HReq
- func (h *HReq) XMLType() *HReq
- type HandleFunc
- type MiddleFunc
- type Middleware
- type RequestCreator
- type RequestCreatorFunc
- type RespDecoder
- type Response
- func ConnectDo(pathURL string) (*Response, error)
- func DeleteDo(pathURL string) (*Response, error)
- func GetDo(pathURL string) (*Response, error)
- func Head(pathURL string) (*Response, error)
- func OptionsDo(pathURL string) (*Response, error)
- func PatchDo(pathURL string, body ...interface{}) (*Response, error)
- func PostDo(pathURL string, body ...interface{}) (*Response, error)
- func PutDo(pathURL string, body ...interface{}) (*Response, error)
- func TraceDo(pathURL string) (*Response, error)
- func (r *Response) BodyString() string
- func (r *Response) ContentType() string
- func (r *Response) Decode(ptr interface{}) error
- func (r *Response) HeaderString() string
- func (r *Response) IsContentType(prefix string) bool
- func (r *Response) IsEmptyBody() bool
- func (r *Response) IsFail() bool
- func (r *Response) IsJSONType() bool
- func (r *Response) IsOK() bool
- func (r *Response) IsSuccessful() bool
- func (r *Response) Result() *http.Response
- func (r *Response) SetDecoder(decoder RespDecoder)
- func (r *Response) String() string
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type BodyProvider ¶
type BodyProvider interface {
// ContentType returns the Content-Type of the body.
ContentType() string
// Body returns the io.Reader body.
Body() (io.Reader, error)
}
BodyProvider provides Body content for http.Request attachment.
type HReq ¶
type HReq struct {
// contains filtered or unexported fields
}
HReq is an HTTP Request builder and sender.
func (*HReq) AddHeader ¶
AddHeader adds the key, value pair in Headers, appending values for existing keys to the key's values. Header keys are canonicalized.
func (*HReq) AddHeaders ¶
AddHeaders adds all the http.Header values, appending values for existing keys to the key's values. Header keys are canonicalized.
func (*HReq) BasicAuth ¶
BasicAuth sets the Authorization header to use HTTP Basic Authentication with the provided username and password. With HTTP Basic Authentication the provided username and password are not encrypted.
func (*HReq) BodyProvider ¶
func (h *HReq) BodyProvider(bp BodyProvider) *HReq
BodyProvider with custom body provider
func (*HReq) BodyReader ¶
BodyReader with custom io reader body
func (*HReq) ConfigHClient ¶
ConfigHClient custom config http client.
Usage:
h.ConfigHClient(func(hClient *http.Client) {
hClient.Timeout = 30 * time.Second
})
func (*HReq) Connect ¶
Connect sets the method to CONNECT and sets the given pathURL, then send request and return http response.
func (*HReq) ConnectDo ¶
ConnectDo sets the method to CONNECT and sets the given pathURL, then send request and return http response.
func (*HReq) ContentType ¶
ContentType with custom ContentType header
Usage:
// json type h.ContentType(httpctype.JSON) // form type h.ContentType(httpctype.Form)
func (*HReq) DeleteDo ¶
DeleteDo sets the method to DELETE and sets the given pathURL, then send request and return http response.
func (*HReq) Doer ¶
Doer custom set http request doer. If a nil client is given, the http.DefaultClient will be used.
func (*HReq) FileContentsBody ¶
FileContentsBody read file contents as body
func (*HReq) GetDo ¶
GetDo sets the method to GET and sets the given pathURL, then send request and return response.
func (*HReq) Head ¶
Head sets the method to HEAD and request the pathURL, then send request and return response.
func (*HReq) HeadDo ¶
HeadDo sets the method to HEAD and request the pathURL, then send request and return response.
func (*HReq) HttpClient ¶
HttpClient custom set http client as request doer
func (*HReq) Middleware ¶
func (h *HReq) Middleware(middles ...Middleware) *HReq
Middleware add one or multi middlewares
func (*HReq) Middlewares ¶
func (h *HReq) Middlewares(middles ...Middleware) *HReq
Middlewares add one or multi middlewares
func (*HReq) MultipartType ¶
MultipartType with multipart/form-data Content-Type header
func (*HReq) NewRequest ¶
NewRequest build new request
func (*HReq) NewRequestWithCtx ¶
func (h *HReq) NewRequestWithCtx(ctx context.Context, pathURLAndMethod ...string) (*http.Request, error)
NewRequestWithCtx build new request with context
func (*HReq) Options ¶
Options sets the method to OPTIONS and request the pathURL, then send request and return response.
func (*HReq) OptionsDo ¶
OptionsDo sets the method to OPTIONS and request the pathURL, then send request and return response.
func (*HReq) PatchDo ¶
PatchDo sets the method to PATCH and sets the given pathURL, then send request and return http response.
func (*HReq) PostDo ¶
PostDo sets the method to POST and sets the given pathURL, then send request and return http response.
func (*HReq) PutDo ¶
PutDo sets the method to PUT and sets the given pathURL, then send request and return http response.
func (*HReq) QueryParam ¶
QueryParam appends new k-v param to the query string.
func (*HReq) QueryParams ¶
QueryParams appends url.Values/map[string]string to the query string. The value will be encoded as url query parameters on send requests (see Send()).
func (*HReq) QueryValues ¶
QueryValues appends url.Values to the query string. The value will be encoded as url query parameters on new requests (see Send()).
func (*HReq) SendRequest ¶
SendRequest send request
func (*HReq) SendWithCtx ¶
SendWithCtx request with context, then return response
func (*HReq) SetCookieString ¶
SetCookieString set cookie header value.
Usage:
h.New().
SetCookieString("name=inhere;age=30").
GetDo("/some/api")
func (*HReq) SetCookies ¶
SetCookies to request
func (*HReq) SetHeader ¶
SetHeader sets the key, value pair in Headers, replacing existing values associated with key. Header keys are canonicalized.
func (*HReq) SetHeaders ¶
SetHeaders sets all the http.Header values, replacing values for existing keys to the key's values. Header keys are canonicalized.
func (*HReq) StringBody ¶
StringBody with custom string body
func (*HReq) Trace ¶
Trace sets the method to TRACE and sets the given pathURL, then send request and return http response.
func (*HReq) TraceDo ¶
TraceDo sets the method to TRACE and sets the given pathURL, then send request and return http response.
type HandleFunc ¶
HandleFunc for the Middleware
type MiddleFunc ¶
type MiddleFunc func(r *http.Request, next HandleFunc) (*Response, error)
MiddleFunc implements the Middleware interface
func (MiddleFunc) Handle ¶
func (mf MiddleFunc) Handle(r *http.Request, next HandleFunc) (*Response, error)
Handle request
type Middleware ¶
type Middleware interface {
Handle(r *http.Request, next HandleFunc) (*Response, error)
}
Middleware interface for client request.
type RequestCreator ¶
RequestCreator interface
type RequestCreatorFunc ¶
RequestCreatorFunc func
type RespDecoder ¶
type RespDecoder interface {
// Decode decodes the response into the value pointed to by ptr.
Decode(resp *http.Response, ptr interface{}) error
}
RespDecoder decodes http responses into struct values.
type Response ¶
Response is a http.Response wrapper
func ConnectDo ¶
ConnectDo sets the method to CONNECT and sets the given pathURL, then send request and return http response.
func DeleteDo ¶
DeleteDo sets the method to DELETE and sets the given pathURL, then send request and return http response.
func GetDo ¶
GetDo sets the method to GET and sets the given pathURL, then send request and return response.
func Head ¶
Head sets the method to HEAD and request the pathURL, then send request and return response.
func OptionsDo ¶
OptionsDo sets the method to OPTIONS and request the pathURL, then send request and return response.
func PatchDo ¶
PatchDo sets the method to PATCH and sets the given pathURL, then send request and return http response.
func PostDo ¶
PostDo sets the method to POST and sets the given pathURL, then send request and return http response.
func PutDo ¶
PutDo sets the method to PUT and sets the given pathURL, then send request and return http response.
func TraceDo ¶
TraceDo sets the method to TRACE and sets the given pathURL, then send request and return http response.
func (*Response) BodyString ¶
BodyString convert response body to string
func (*Response) ContentType ¶
ContentType get response content type
func (*Response) HeaderString ¶
HeaderString convert response headers to string
func (*Response) IsContentType ¶
IsContentType check response content type is equals the given.
Usage:
resp, err := hreq.Post("some.host/path")
ok := resp.IsContentType("application/xml")
func (*Response) IsEmptyBody ¶
IsEmptyBody check response body is empty
func (*Response) IsJSONType ¶
IsJSONType check response content type is JSON
func (*Response) IsSuccessful ¶
IsSuccessful check response status code is in 200 - 300
func (*Response) SetDecoder ¶
func (r *Response) SetDecoder(decoder RespDecoder)
SetDecoder for response