Documentation
¶
Overview ¶
Package greq is a simple http cli request builder, inspired from https://github.com/dghubble/sling
Index ¶
- Constants
- Variables
- func MergeURLValues(uv gourl.Values, values ...any) gourl.Values
- func SetHeaderMap(req *http.Request, headerMap map[string]string)
- func SetHeaders(req *http.Request, headers ...http.Header)
- type AfterSendFn
- type BodyProvider
- type Builder
- func (b *Builder) AddHeader(key, value string) *Builder
- func (b *Builder) AddHeaderMap(headers map[string]string) *Builder
- func (b *Builder) AddHeaders(headers http.Header) *Builder
- func (b *Builder) AddQuery(key string, value any) *Builder
- func (b *Builder) AnyBody(body any) *Builder
- func (b *Builder) BasicAuth(username, password string) *Builder
- func (b *Builder) BodyProvider(bp BodyProvider) *Builder
- func (b *Builder) BodyReader(r io.Reader) *Builder
- func (b *Builder) Build(method, pathURL string) (*http.Request, error)
- func (b *Builder) BytesBody(bs []byte) *Builder
- func (b *Builder) Delete(pathURL string, optFns ...OptionFn) *Builder
- func (b *Builder) DeleteDo(pathURL string, optFns ...OptionFn) (*Response, error)
- func (b *Builder) Do(optFns ...OptionFn) (*Response, error)
- func (b *Builder) FileContentsBody(filePath string) *Builder
- func (b *Builder) FormBody(formData any) *Builder
- func (b *Builder) FormType() *Builder
- func (b *Builder) Get(pathURL string, optFns ...OptionFn) *Builder
- func (b *Builder) GetDo(pathURL string, optFns ...OptionFn) (*Response, error)
- func (b *Builder) JSONBody(jsonData any) *Builder
- func (b *Builder) JSONType() *Builder
- func (b *Builder) Multipart(key, value string) *Builder
- func (b *Builder) MultipartType() *Builder
- func (b *Builder) Patch(pathURL string, data any, optFns ...OptionFn) *Builder
- func (b *Builder) PatchDo(pathURL string, data any, optFns ...OptionFn) (*Response, error)
- func (b *Builder) PathURL(pathURL string) *Builder
- func (b *Builder) Post(pathURL string, data any, optFns ...OptionFn) *Builder
- func (b *Builder) PostDo(pathURL string, data any, optFns ...OptionFn) (*Response, error)
- func (b *Builder) Put(pathURL string, data any, optFns ...OptionFn) *Builder
- func (b *Builder) PutDo(pathURL string, data any, optFns ...OptionFn) (*Response, error)
- func (b *Builder) QueryParams(ps any) *Builder
- func (b *Builder) QueryValues(values gourl.Values) *Builder
- func (b *Builder) Send(method, url string, optFns ...OptionFn) (*Response, error)
- func (b *Builder) SetHeader(key, value string) *Builder
- func (b *Builder) SetHeaderMap(headers map[string]string) *Builder
- func (b *Builder) SetHeaders(headers http.Header) *Builder
- func (b *Builder) String() string
- func (b *Builder) StringBody(s string) *Builder
- func (b *Builder) UserAgent(ua string) *Builder
- func (b *Builder) UserAuth(value string) *Builder
- func (b *Builder) WithBody(body any) *Builder
- func (b *Builder) WithClient(c *Client) *Builder
- func (b *Builder) WithContentType(value string) *Builder
- func (b *Builder) WithCookieString(value string) *Builder
- func (b *Builder) WithCookies(hcs ...*http.Cookie) *Builder
- func (b *Builder) WithMethod(method string) *Builder
- func (b *Builder) WithOptionFn(fns ...OptionFn) *Builder
- func (b *Builder) WithOptionFns(fns []OptionFn) *Builder
- func (b *Builder) WithQuerySMap(smp map[string]string) *Builder
- func (b *Builder) WithType(value string) *Builder
- func (b *Builder) XMLType() *Builder
- type Client
- func (h *Client) AnyBody(body any) *Builder
- func (h *Client) BaseURL(baseURL string) *Client
- func (h *Client) BasicAuth(username, password string) *Builder
- func (h *Client) Body(body any) *Builder
- func (h *Client) BodyProvider(bp BodyProvider) *Builder
- func (h *Client) BodyReader(r io.Reader) *Builder
- func (h *Client) Builder(optFns ...OptionFn) *Builder
- func (h *Client) Client(doer httpreq.Doer) *Client
- func (h *Client) Config(fn func(doer httpreq.Doer)) *Client
- func (h *Client) ConfigHClient(fn func(hClient *http.Client)) *Client
- func (h *Client) DefaultBasicAuth(username, password string) *Client
- func (h *Client) DefaultContentType(value string) *Client
- func (h *Client) DefaultHeader(key, value string) *Client
- func (h *Client) DefaultHeaders(headers http.Header) *Client
- func (h *Client) DefaultMethod(method string) *Client
- func (h *Client) DefaultUserAgent(value string) *Client
- func (h *Client) DefaultUserAuth(value string) *Client
- func (h *Client) Delete(pathURL string) *Builder
- func (h *Client) DeleteDo(pathURL string, optFns ...OptionFn) (*Response, error)
- func (h *Client) Do(method, url string, optFns ...OptionFn) (*Response, error)
- func (h *Client) DoWithOption(method, url string, optFns ...OptionFn) (*Response, error)
- func (h *Client) Doer(doer httpreq.Doer) *Client
- func (h *Client) FormType() *Builder
- func (h *Client) Get(pathURL string) *Builder
- func (h *Client) GetDo(pathURL string, optFns ...OptionFn) (*Response, error)
- func (h *Client) Head(pathURL string) *Builder
- func (h *Client) HeadDo(pathURL string, optFns ...OptionFn) (*Response, error)
- func (h *Client) HttpClient(hClient *http.Client) *Client
- func (h *Client) JSONType() *Builder
- func (h *Client) Middleware(middles ...Middleware) *Client
- func (h *Client) Middlewares(middles ...Middleware) *Client
- func (h *Client) MustSend(method, url string, optFns ...OptionFn) *Response
- func (h *Client) NewRequest(method, url string, optFns ...OptionFn) (*http.Request, error)
- func (h *Client) NewRequestWithOptions(url string, opt *Options) (*http.Request, error)
- func (h *Client) OnBeforeSend(fn func(r *http.Request)) *Client
- func (h *Client) Patch(pathURL string) *Builder
- func (h *Client) PatchDo(pathURL string, optFns ...OptionFn) (*Response, error)
- func (h *Client) Post(pathURL string) *Builder
- func (h *Client) PostDo(pathURL string, optFns ...OptionFn) (*Response, error)
- func (h *Client) Put(pathURL string) *Builder
- func (h *Client) PutDo(pathURL string, optFns ...OptionFn) (*Response, error)
- func (h *Client) QueryParams(ps any) *Builder
- func (h *Client) Send(method, url string, optFns ...OptionFn) (*Response, error)
- func (h *Client) SendRaw(raw string, varMp map[string]string) (*Response, error)
- func (h *Client) SendRequest(req *http.Request) (*Response, error)
- func (h *Client) SendWithOpt(pathURL string, opt *Options) (*Response, error)
- func (h *Client) SendWithOption(method, url string, optFns ...OptionFn) (*Response, error)
- func (h *Client) String() string
- func (h *Client) Sub() *Client
- func (h *Client) Use(middles ...Middleware) *Client
- func (h *Client) UserAgent(value string) *Builder
- func (h *Client) UserAuth(value string) *Builder
- func (h *Client) Uses(middles ...Middleware) *Client
- func (h *Client) WithContentType(value string) *Builder
- func (h *Client) WithRespDecoder(respDecoder RespDecoder) *Client
- type HandleFunc
- type MiddleFunc
- type Middleware
- type OptionFn
- type Options
- type RequestCreator
- type RequestCreatorFunc
- type RespDecoder
- type Response
- func ConnectDo(pathURL string, optFns ...OptionFn) (*Response, error)
- func DeleteDo(pathURL string, optFns ...OptionFn) (*Response, error)
- func GetDo(pathURL string, optFns ...OptionFn) (*Response, error)
- func HeadDo(pathURL string, optFns ...OptionFn) (*Response, error)
- func Must(w *Response, err error) *Response
- func MustDo(method, pathURL string, optFns ...OptionFn) *Response
- func OptionsDo(pathURL string, optFns ...OptionFn) (*Response, error)
- func PatchDo(pathURL string, data any, optFns ...OptionFn) (*Response, error)
- func PostDo(pathURL string, data any, optFns ...OptionFn) (*Response, error)
- func PutDo(pathURL string, data any, optFns ...OptionFn) (*Response, error)
- func SendDo(method, pathURL string, optFns ...OptionFn) (*Response, error)
- func TraceDo(pathURL string, optFns ...OptionFn) (*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 any) 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) SaveFile(file string) (err error)
- func (r *Response) SetDecoder(decoder RespDecoder)
- func (r *Response) String() string
- type XmlDecoder
Constants ¶
const ( HeaderUAgent = "User-Agent" HeaderAuth = "Authorization" AgentCURL = "CURL/7.64.1 greq/1.0.2" )
Variables ¶
var DefaultDoer = http.DefaultClient
DefaultDoer for request.
Functions ¶
func MergeURLValues ¶ added in v0.3.0
MergeURLValues merge url.Values by overwrite.
values support: url.Values, map[string]string, map[string][]string
TODO replace to goutil/netutil/httpreq.MergeURLValues
func SetHeaderMap ¶ added in v0.3.0
SetHeaderMap to reqeust instance.
TODO replace to goutil/netutil/httpreq.SetHeaderMap
Types ¶
type AfterSendFn ¶ added in v0.3.0
AfterSendFn callback func
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 Builder ¶ added in v0.3.0
type Builder struct {
*Options
// contains filtered or unexported fields
}
Builder is a http request builder.
func BuilderWithClient ¶ added in v0.3.0
BuilderWithClient create a new builder with client
func (*Builder) AddHeader ¶ added in v0.3.0
AddHeader adds the key, value pair in HeaderM, appending values for existing keys to the key's values. Header keys are canonicalized.
func (*Builder) AddHeaderMap ¶ added in v0.3.0
AddHeaderMap sets all the http.Header values, appending values for existing keys to the key's values.
func (*Builder) AddHeaders ¶ added in v0.3.0
AddHeaders adds all the http.Header values, appending values for existing keys to the key's values. Header keys are canonicalized.
func (*Builder) BasicAuth ¶ added in v0.3.0
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 (*Builder) BodyProvider ¶ added in v0.3.0
func (b *Builder) BodyProvider(bp BodyProvider) *Builder
BodyProvider with custom body provider
func (*Builder) BodyReader ¶ added in v0.3.0
BodyReader with custom io reader body
func (*Builder) Delete ¶ added in v0.3.0
Delete sets the method to DELETE and sets the given pathURL
func (*Builder) DeleteDo ¶ added in v0.3.0
DeleteDo sets the method to DELETE and sets the given pathURL, then send request and return response.
func (*Builder) FileContentsBody ¶ added in v0.3.0
FileContentsBody read file contents as body
func (*Builder) GetDo ¶ added in v0.3.0
GetDo sets the method to GET and sets the given pathURL, then send request and return response.
func (*Builder) MultipartType ¶ added in v0.3.0
MultipartType with multipart/form-data Content-Type header
func (*Builder) PatchDo ¶ added in v0.3.0
PatchDo sets the method to PATCH and sets the given pathURL, then send request and return response.
func (*Builder) PostDo ¶ added in v0.3.0
PostDo sets the method to POST and sets the given pathURL, then send request and return http response.
func (*Builder) PutDo ¶ added in v0.3.0
PutDo sets the method to PUT and sets the given pathURL, then send request and return response.
func (*Builder) QueryParams ¶ added in v0.3.0
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 (*Builder) QueryValues ¶ added in v0.3.0
QueryValues appends url.Values to the Query string. The value will be encoded as url Query parameters on new requests (see Send()).
func (*Builder) SetHeader ¶ added in v0.3.0
SetHeader sets the key, value pair in HeaderM, replacing existing values associated with key. Header keys are canonicalized.
func (*Builder) SetHeaderMap ¶ added in v0.3.0
SetHeaderMap sets all the http.Header values, replacing values for existing keys to the key's values.
func (*Builder) SetHeaders ¶ added in v0.3.0
SetHeaders sets all the http.Header values, replacing values for existing keys to the key's values. Header keys are canonicalized.
func (*Builder) StringBody ¶ added in v0.3.0
StringBody with custom string body
func (*Builder) WithClient ¶ added in v0.3.0
WithClient set cli to builder
func (*Builder) WithContentType ¶ added in v0.3.0
WithContentType with custom ContentType header
func (*Builder) WithCookieString ¶ added in v0.3.0
WithCookieString set cookie header value.
Usage:
h.NewOpt().
WithCookieString("name=inhere;age=30").
Do("/some/api", "GET")
func (*Builder) WithCookies ¶ added in v0.3.0
WithCookies to request
func (*Builder) WithMethod ¶ added in v0.3.0
WithMethod set request method name.
func (*Builder) WithOptionFn ¶ added in v0.3.0
WithOptionFn set option fns to builder
func (*Builder) WithOptionFns ¶ added in v0.3.0
WithOptionFns set option fns to builder
func (*Builder) WithQuerySMap ¶ added in v0.3.0
WithQuerySMap appends map[string]string to the Query string.
type Client ¶ added in v0.3.0
type Client struct {
// Vars template vars for URL, Header, Query, Body
//
// eg: http://example.com/{name}
Vars map[string]string
// BeforeSend callback
BeforeSend func(r *http.Request)
// AfterSend callback
AfterSend AfterSendFn
// content type
ContentType string
// contains filtered or unexported fields
}
Client is an HTTP Request builder and sender.
func (*Client) BasicAuth ¶ added in v0.3.0
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 (*Client) BodyProvider ¶ added in v0.3.0
func (h *Client) BodyProvider(bp BodyProvider) *Builder
BodyProvider with custom body provider
func (*Client) BodyReader ¶ added in v0.3.0
BodyReader with custom io reader body
func (*Client) ConfigHClient ¶ added in v0.3.0
ConfigHClient custom config http cli.
Usage:
h.ConfigHClient(func(hClient *http.Client) {
hClient.Timeout = 30 * time.Second
})
func (*Client) DefaultBasicAuth ¶ added in v0.3.0
DefaultBasicAuth 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 (*Client) DefaultContentType ¶ added in v0.3.0
DefaultContentType set default ContentType header, it will be used for all requests.
Usage:
// json type h.DefaultContentType(httpctype.JSON) // form type h.DefaultContentType(httpctype.Form)
func (*Client) DefaultHeader ¶ added in v0.3.0
DefaultHeader sets the http.Header value, it will be used for all requests.
func (*Client) DefaultHeaders ¶ added in v0.3.0
DefaultHeaders sets all the http.Header values, it will be used for all requests.
func (*Client) DefaultMethod ¶ added in v0.3.0
DefaultMethod set default method name. it will be used when the Get()/Post() method is empty.
func (*Client) DefaultUserAgent ¶ added in v0.3.0
DefaultUserAgent with User-Agent header setting for all requests.
func (*Client) DefaultUserAuth ¶ added in v0.3.0
DefaultUserAuth with user auth header value for all requests.
func (*Client) DeleteDo ¶ added in v0.3.0
DeleteDo sets the method to DELETE and sets the given pathURL, then send request and return http response.
func (*Client) DoWithOption ¶ added in v0.3.0
DoWithOption request with options, then return response
func (*Client) Doer ¶ added in v0.3.0
Doer custom set http request doer. If a nil cli is given, the DefaultDoer will be used.
func (*Client) GetDo ¶ added in v0.3.0
GetDo sets the method to GET and sets the given pathURL, then send request and return response.
func (*Client) Head ¶ added in v0.3.0
Head sets the method to HEAD and request the pathURL, then send request and return response.
func (*Client) HeadDo ¶ added in v0.3.0
HeadDo sets the method to HEAD and request the pathURL, then send request and return response.
func (*Client) HttpClient ¶ added in v0.3.0
HttpClient custom set http cli as request doer
func (*Client) Middleware ¶ added in v0.3.0
func (h *Client) Middleware(middles ...Middleware) *Client
Middleware add one or multi middlewares
func (*Client) Middlewares ¶ added in v0.3.0
func (h *Client) Middlewares(middles ...Middleware) *Client
Middlewares add one or multi middlewares
func (*Client) MustSend ¶ added in v0.3.0
MustSend send request and return response, will panic on error
func (*Client) NewRequest ¶ added in v0.3.0
NewRequest build new request
func (*Client) NewRequestWithOptions ¶ added in v0.3.0
NewRequestWithOptions build new request with Options
func (*Client) OnBeforeSend ¶ added in v0.3.0
OnBeforeSend for cli
func (*Client) PatchDo ¶ added in v0.3.0
PatchDo sets the method to PATCH and sets the given pathURL, then send request and return http response.
func (*Client) PostDo ¶ added in v0.3.0
PostDo sets the method to POST and sets the given pathURL, then send request and return http response.
func (*Client) PutDo ¶ added in v0.3.0
PutDo sets the method to PUT and sets the given pathURL, then send request and return http response.
func (*Client) QueryParams ¶ added in v0.3.0
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 (*Client) SendRaw ¶ added in v0.3.0
SendRaw http request text.
Format:
POST https://example.com/path?name=inhere Content-Type: application/json Accept: */* <content>
func (*Client) SendRequest ¶ added in v0.3.0
SendRequest send request
func (*Client) SendWithOpt ¶ added in v0.3.0
SendWithOpt send request with option, then return response
func (*Client) SendWithOption ¶ added in v0.3.0
SendWithOption request with options, then return response
func (*Client) Use ¶ added in v0.3.0
func (h *Client) Use(middles ...Middleware) *Client
Use one or multi middlewares
func (*Client) UserAgent ¶ added in v0.3.0
UserAgent with User-Agent header setting for all requests.
func (*Client) Uses ¶ added in v0.3.0
func (h *Client) Uses(middles ...Middleware) *Client
Uses one or multi middlewares
func (*Client) WithContentType ¶ added in v0.3.0
WithContentType with custom Content-Type header
func (*Client) WithRespDecoder ¶ added in v0.3.0
func (h *Client) WithRespDecoder(respDecoder RespDecoder) *Client
WithRespDecoder for cli
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 cli request.
type OptionFn ¶ added in v0.3.0
type OptionFn func(opt *Options)
OptionFn for config request options
func WithContentType ¶ added in v0.3.0
WithContentType set content-type
func WithUserAgent ¶ added in v0.3.0
WithUserAgent set user-agent header
type Options ¶ added in v0.3.0
type Options struct {
// Method for request
Method string
// ContentType header
ContentType string
// Headers for request
Header http.Header
// HeaderM map string data.
HeaderM map[string]string
// Query params data.
Query gourl.Values
QueryM map[string]any
// Data for request, will be encoded to query string or req body.
//
// type allow: string, []byte, io.Reader, io.ReadCloser, url.Values, map[string]string
Data any
// Body data for request, only for POST, PUT, PATCH
//
// type allow: string, []byte, io.Reader, io.ReadCloser, url.Values, map[string]string
Body any
// Provider body data provider, can with custom content-type
Provider BodyProvider
// EncodeJSON req body
EncodeJSON bool
// Timeout unit: ms
Timeout int
// TCancelFn will auto set it on Timeout > 0
TCancelFn context.CancelFunc
// Context for request
Context context.Context
// Logger for request
Logger httpreq.ReqLogger
// contains filtered or unexported fields
}
Options for a request build
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 any) 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 HeadDo ¶ added in v0.3.0
HeadDo sets the method to HEAD and request the pathURL, then send request and return response.
func MustDo ¶ added in v0.3.0
MustDo sets the method to POST and sets the given pathURL, then send request and return http 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 SendDo ¶ added in v0.3.0
SendDo sets the method to POST 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.