Documentation
¶
Overview ¶
Package greq is a simple http client request builder, inspired from https://github.com/dghubble/sling
Index ¶
- Variables
- func ToString(r *http.Request, err error) string
- type BodyProvider
- type HandleFunc
- type HiReq
- func (h *HiReq) AddHeader(key, value string) *HiReq
- func (h *HiReq) AddHeaders(headers http.Header) *HiReq
- func (h *HiReq) BaseURL(baseURL string) *HiReq
- func (h *HiReq) BasicAuth(username, password string) *HiReq
- func (h *HiReq) Body(body interface{}) *HiReq
- func (h *HiReq) BodyProvider(bp BodyProvider) *HiReq
- func (h *HiReq) BodyReader(r io.Reader) *HiReq
- func (h *HiReq) Build(pathURLAndMethod ...string) (*http.Request, error)
- func (h *HiReq) BytesBody(bs []byte) *HiReq
- func (h *HiReq) Client(doer httpreq.Doer) *HiReq
- func (h *HiReq) Config(fn func(doer httpreq.Doer)) *HiReq
- func (h *HiReq) ConfigHClient(fn func(hClient *http.Client)) *HiReq
- func (h *HiReq) Connect(pathURL string) (*Response, error)
- func (h *HiReq) ConnectDo(pathURL string) (*Response, error)
- func (h *HiReq) ContentType(value string) *HiReq
- func (h *HiReq) Delete(pathURL string) *HiReq
- func (h *HiReq) DeleteDo(pathURL string) (*Response, error)
- func (h *HiReq) Do(pathURLAndMethod ...string) (*Response, error)
- func (h *HiReq) DoWithCtx(ctx context.Context, pathURLAndMethod ...string) (*Response, error)
- func (h *HiReq) Doer(doer httpreq.Doer) *HiReq
- func (h *HiReq) FileContentsBody(filepath string) *HiReq
- func (h *HiReq) FormBody(formData interface{}) *HiReq
- func (h *HiReq) FormType() *HiReq
- func (h *HiReq) Get(pathURL string) *HiReq
- func (h *HiReq) GetDo(pathURL string) (*Response, error)
- func (h *HiReq) Head(pathURL string) *HiReq
- func (h *HiReq) HeadDo(pathURL string) (*Response, error)
- func (h *HiReq) HttpClient(hClient *http.Client) *HiReq
- func (h *HiReq) JSONBody(jsonData interface{}) *HiReq
- func (h *HiReq) JSONType() *HiReq
- func (h *HiReq) Method(method string) *HiReq
- func (h *HiReq) Middleware(middles ...Middleware) *HiReq
- func (h *HiReq) Middlewares(middles ...Middleware) *HiReq
- func (h *HiReq) Multipart(key, value string) *HiReq
- func (h *HiReq) MultipartType() *HiReq
- func (h *HiReq) MustSend(pathURLAndMethod ...string) *Response
- func (h *HiReq) New() *HiReq
- func (h *HiReq) NewRequest(pathURLAndMethod ...string) (*http.Request, error)
- func (h *HiReq) NewRequestWithCtx(ctx context.Context, pathURLAndMethod ...string) (*http.Request, error)
- func (h *HiReq) Options(pathURL string) *HiReq
- func (h *HiReq) OptionsDo(pathURL string) (*Response, error)
- func (h *HiReq) Patch(pathURL string) *HiReq
- func (h *HiReq) PatchDo(pathURL string) (*Response, error)
- func (h *HiReq) PathURL(pathURL string) *HiReq
- func (h *HiReq) Post(pathURL string) *HiReq
- func (h *HiReq) PostDo(pathURL string) (*Response, error)
- func (h *HiReq) Put(pathURL string) *HiReq
- func (h *HiReq) PutDo(pathURL string) (*Response, error)
- func (h *HiReq) QueryParam(key string, value interface{}) *HiReq
- func (h *HiReq) QueryParams(ps interface{}) *HiReq
- func (h *HiReq) QueryValues(values url.Values) *HiReq
- func (h *HiReq) Send(pathURLAndMethod ...string) (*Response, error)
- func (h *HiReq) SendRaw(raw string) (*Response, error)
- func (h *HiReq) SendRequest(req *http.Request) (*Response, error)
- func (h *HiReq) SendWithCtx(ctx context.Context, pathURLAndMethod ...string) (*Response, error)
- func (h *HiReq) SetCookieString(value string) *HiReq
- func (h *HiReq) SetCookies(hcs ...*http.Cookie) *HiReq
- func (h *HiReq) SetHeader(key, value string) *HiReq
- func (h *HiReq) SetHeaders(headers http.Header) *HiReq
- func (h *HiReq) String() string
- func (h *HiReq) StringBody(s string) *HiReq
- func (h *HiReq) Trace(pathURL string) *HiReq
- func (h *HiReq) TraceDo(pathURL string) (*Response, error)
- func (h *HiReq) Use(middles ...Middleware) *HiReq
- func (h *HiReq) UserAgent(value string) *HiReq
- func (h *HiReq) UserAuth(value string) *HiReq
- func (h *HiReq) Uses(middles ...Middleware) *HiReq
- func (h *HiReq) WithRespDecoder(respDecoder RespDecoder) *HiReq
- func (h *HiReq) XMLType() *HiReq
- 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) BodyBuffer() *bytes.Buffer
- func (r *Response) BodyString() string
- func (r *Response) CloseBody() error
- 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) QuietCloseBody()
- func (r *Response) Result() *http.Response
- func (r *Response) SetDecoder(decoder RespDecoder)
- func (r *Response) String() string
- type XmlDecoder
Constants ¶
This section is empty.
Variables ¶
var DefaultDoer = http.DefaultClient
DefaultDoer for request.
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 HandleFunc ¶
HandleFunc for the Middleware
type HiReq ¶
type HiReq struct {
// contains filtered or unexported fields
}
HiReq is an HTTP Request builder and sender.
func (*HiReq) AddHeader ¶
AddHeader adds the key, value pair in Headers, appending values for existing keys to the key's values. Header keys are canonicalized.
func (*HiReq) AddHeaders ¶
AddHeaders adds all the http.Header values, appending values for existing keys to the key's values. Header keys are canonicalized.
func (*HiReq) 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 (*HiReq) BodyProvider ¶
func (h *HiReq) BodyProvider(bp BodyProvider) *HiReq
BodyProvider with custom body provider
func (*HiReq) BodyReader ¶
BodyReader with custom io reader body
func (*HiReq) ConfigHClient ¶
ConfigHClient custom config http client.
Usage:
h.ConfigHClient(func(hClient *http.Client) {
hClient.Timeout = 30 * time.Second
})
func (*HiReq) Connect ¶
Connect sets the method to CONNECT and sets the given pathURL, then send request and return http response.
func (*HiReq) ConnectDo ¶
ConnectDo sets the method to CONNECT and sets the given pathURL, then send request and return http response.
func (*HiReq) ContentType ¶
ContentType with custom ContentType header
Usage:
// json type h.ContentType(httpctype.JSON) // form type h.ContentType(httpctype.Form)
func (*HiReq) DeleteDo ¶
DeleteDo sets the method to DELETE and sets the given pathURL, then send request and return http response.
func (*HiReq) Doer ¶
Doer custom set http request doer. If a nil client is given, the DefaultDoer will be used.
func (*HiReq) FileContentsBody ¶
FileContentsBody read file contents as body
func (*HiReq) GetDo ¶
GetDo sets the method to GET and sets the given pathURL, then send request and return response.
func (*HiReq) Head ¶
Head sets the method to HEAD and request the pathURL, then send request and return response.
func (*HiReq) HeadDo ¶
HeadDo sets the method to HEAD and request the pathURL, then send request and return response.
func (*HiReq) HttpClient ¶
HttpClient custom set http client as request doer
func (*HiReq) Middleware ¶
func (h *HiReq) Middleware(middles ...Middleware) *HiReq
Middleware add one or multi middlewares
func (*HiReq) Middlewares ¶
func (h *HiReq) Middlewares(middles ...Middleware) *HiReq
Middlewares add one or multi middlewares
func (*HiReq) MultipartType ¶
MultipartType with multipart/form-data Content-Type header
func (*HiReq) NewRequest ¶
NewRequest build new request
func (*HiReq) NewRequestWithCtx ¶
func (h *HiReq) NewRequestWithCtx(ctx context.Context, pathURLAndMethod ...string) (*http.Request, error)
NewRequestWithCtx build new request with context
func (*HiReq) Options ¶
Options sets the method to OPTIONS and request the pathURL, then send request and return response.
func (*HiReq) OptionsDo ¶
OptionsDo sets the method to OPTIONS and request the pathURL, then send request and return response.
func (*HiReq) PatchDo ¶
PatchDo sets the method to PATCH and sets the given pathURL, then send request and return http response.
func (*HiReq) PostDo ¶
PostDo sets the method to POST and sets the given pathURL, then send request and return http response.
func (*HiReq) PutDo ¶
PutDo sets the method to PUT and sets the given pathURL, then send request and return http response.
func (*HiReq) QueryParam ¶
QueryParam appends new k-v param to the query string.
func (*HiReq) 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 (*HiReq) QueryValues ¶
QueryValues appends url.Values to the query string. The value will be encoded as url query parameters on new requests (see Send()).
func (*HiReq) SendRequest ¶
SendRequest send request
func (*HiReq) SendWithCtx ¶
SendWithCtx request with context, then return response
func (*HiReq) SetCookieString ¶
SetCookieString set cookie header value.
Usage:
h.New().
SetCookieString("name=inhere;age=30").
GetDo("/some/api")
func (*HiReq) SetCookies ¶
SetCookies to request
func (*HiReq) SetHeader ¶
SetHeader sets the key, value pair in Headers, replacing existing values associated with key. Header keys are canonicalized.
func (*HiReq) SetHeaders ¶
SetHeaders sets all the http.Header values, replacing values for existing keys to the key's values. Header keys are canonicalized.
func (*HiReq) StringBody ¶
StringBody with custom string body
func (*HiReq) Trace ¶
Trace sets the method to TRACE and sets the given pathURL, then send request and return http response.
func (*HiReq) TraceDo ¶
TraceDo sets the method to TRACE and sets the given pathURL, then send request and return http response.
func (*HiReq) Uses ¶
func (h *HiReq) Uses(middles ...Middleware) *HiReq
Uses one or multi middlewares
func (*HiReq) WithRespDecoder ¶
func (h *HiReq) WithRespDecoder(respDecoder RespDecoder) *HiReq
WithRespDecoder for client
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 := greq.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) QuietCloseBody ¶
func (r *Response) QuietCloseBody()
QuietCloseBody close resp body, ignore error
func (*Response) SetDecoder ¶
func (r *Response) SetDecoder(decoder RespDecoder)
SetDecoder for response
type XmlDecoder ¶
type XmlDecoder struct {
}
XmlDecoder decodes http response body into a XML-tagged struct value.