Documentation
¶
Overview ¶
Package greq is a simple http client request builder, support batch requests.
Index ¶
- Constants
- Variables
- func DefaultRetryChecker(resp *Response, err error, attempt int) bool
- func NewTransport(onCreate func(ht *http.Transport)) *http.Transport
- 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) RemoveHeaders(keys ...string) *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) 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(h *Client)) *Client
- func (h *Client) ConfigDoer(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) DefaultTimeout(timeoutMs int) *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) Download(url, savePath string, optFns ...OptionFn) (int, error)
- 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) error) *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) SetMaxIdleConns(maxIdleConns, maxIdleConnsPerHost int) *Client
- func (h *Client) String() string
- func (h *Client) Sub() *Client
- func (h *Client) UploadFile(pathURL, fieldName, filePath string, optFns ...OptionFn) (*Response, error)
- func (h *Client) UploadFiles(pathURL string, files map[string]string, optFns ...OptionFn) (*Response, error)
- func (h *Client) UploadWithData(pathURL string, files map[string]string, fields map[string]string, ...) (*Response, error)
- 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) WithBaseURL(baseURL string) *Client
- func (h *Client) WithContentType(value string) *Builder
- func (h *Client) WithMaxRetries(maxRetries int) *Client
- func (h *Client) WithRespDecoder(respDecoder RespDecoder) *Client
- func (h *Client) WithRetryChecker(checker RetryChecker) *Client
- func (h *Client) WithRetryConfig(maxRetries, retryDelay int, checker RetryChecker) *Client
- func (h *Client) WithRetryDelay(retryDelay int) *Client
- type HandleFunc
- type MiddleFunc
- type Middleware
- type OptionFn
- func WithBody(body any) OptionFn
- func WithContentType(contentType string) OptionFn
- func WithData(data any) OptionFn
- func WithHeader(key, value string) OptionFn
- func WithMaxRetries(maxRetries int) OptionFn
- func WithMethod(method string) OptionFn
- func WithRetry(maxRetries, retryDelay int, checker RetryChecker) OptionFn
- func WithRetryChecker(checker RetryChecker) OptionFn
- func WithRetryDelay(retryDelay int) OptionFn
- func WithTimeout(timeoutMs int) OptionFn
- func WithUserAgent(userAgent string) 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 NewResponse(resp *http.Response, decoder RespDecoder) *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) SaveFile(file string) (n int, err error)
- func (r *Response) SetDecoder(decoder RespDecoder)
- func (r *Response) String() string
- type RetryChecker
- 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 DefaultRetryChecker ¶ added in v0.5.0
DefaultRetryChecker is the default retry condition checker It retries on: - Network errors (err != nil) - 5xx server errors - 429 Too Many Requests
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 an 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) RemoveHeaders ¶ added in v0.5.0
RemoveHeaders removes the keys from HeaderM.
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 {
// Method default http method. default is GET
Method string
// Header default http header. default is nil
Header http.Header
// defalut content type
ContentType string
// BaseURL default base URL. default is ""
BaseURL string
// Timeout default timeout(ms) for each request. default 10s
//
// - 0: not limit
Timeout int
// RespDecoder response data decoder.
// - use for create Response instance. default is JSON decoder
RespDecoder RespDecoder
// ReqVars template vars for request: URL, Header, Query, Body
//
// eg: http://example.com/${name}
ReqVars map[string]string
// BeforeSend callback on each request, can return error to deny request.
BeforeSend func(r *http.Request) error
// AfterSend callback on each request, can use for record request and response
AfterSend AfterSendFn
// MaxRetries max retry times. default is 0 (not retry)
MaxRetries int
// RetryDelay retry delay time (ms). default is 0 (no delay)
RetryDelay int
// RetryChecker retry condition checker. default is nil (not retry)
RetryChecker RetryChecker
// 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) Config ¶ added in v0.3.0
Config custom config for client.
Usage:
h.Config(func(h *Client) {
h.Method = http.MethodPost
})
func (*Client) ConfigDoer ¶ added in v0.5.0
ConfigDoer custom config http request doer
func (*Client) ConfigHClient ¶ added in v0.3.0
ConfigHClient custom config http client.
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) DefaultTimeout ¶ added in v0.5.0
DefaultTimeout set default timeout in milliseconds for requests. default is 0 (infinite)
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) Delete ¶ added in v0.3.0
Delete sets the method to DELETE and sets the given pathURL
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. like IDE .http file contents
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) SetMaxIdleConns ¶ added in v0.5.0
SetMaxIdleConns Set the maximum number of idle connections.
func (*Client) UploadFile ¶ added in v0.5.0
func (h *Client) UploadFile(pathURL, fieldName, filePath string, optFns ...OptionFn) (*Response, error)
UploadFile uploads a single file to the given URL.
func (*Client) UploadFiles ¶ added in v0.5.0
func (h *Client) UploadFiles(pathURL string, files map[string]string, optFns ...OptionFn) (*Response, error)
UploadFiles uploads multiple files to the given URL.
func (*Client) UploadWithData ¶ added in v0.5.0
func (h *Client) UploadWithData(pathURL string, files map[string]string, fields map[string]string, optFns ...OptionFn) (*Response, error)
UploadWithData uploads files with additional form fields.
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) WithBaseURL ¶ added in v0.5.0
WithBaseURL set default base URL for all request
func (*Client) WithContentType ¶ added in v0.3.0
WithContentType with custom Content-Type header
func (*Client) WithMaxRetries ¶ added in v0.5.0
WithMaxRetries set max retry times
func (*Client) WithRespDecoder ¶ added in v0.3.0
func (h *Client) WithRespDecoder(respDecoder RespDecoder) *Client
WithRespDecoder for cli
func (*Client) WithRetryChecker ¶ added in v0.5.0
func (h *Client) WithRetryChecker(checker RetryChecker) *Client
WithRetryChecker set custom retry checker function
func (*Client) WithRetryConfig ¶ added in v0.5.0
func (h *Client) WithRetryConfig(maxRetries, retryDelay int, checker RetryChecker) *Client
WithRetryConfig set retry configuration
func (*Client) WithRetryDelay ¶ added in v0.5.0
WithRetryDelay set retry delay time in milliseconds
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 WithHeader ¶ added in v0.5.0
WithHeader set header
func WithMaxRetries ¶ added in v0.5.0
WithMaxRetries set max retry times for the request
func WithRetry ¶ added in v0.5.0
func WithRetry(maxRetries, retryDelay int, checker RetryChecker) OptionFn
WithRetry set retry configuration for the request
func WithRetryChecker ¶ added in v0.5.0
func WithRetryChecker(checker RetryChecker) OptionFn
WithRetryChecker set custom retry checker function for the request
func WithRetryDelay ¶ added in v0.5.0
WithRetryDelay set retry delay time in milliseconds for the request
func WithTimeout ¶ added in v0.5.0
WithTimeout set timeout (ms)
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 request 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
// Retry configuration
MaxRetries int
RetryDelay int
RetryChecker RetryChecker
// contains filtered or unexported fields
}
Options for one 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 ¶
type Response struct {
// raw http.Response
*http.Response
// CostTime for a request-response. unit: ms
CostTime int64
// contains filtered or unexported fields
}
Response is a http.Response wrapper, add some useful methods.
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 NewResponse ¶ added in v0.5.0
func NewResponse(resp *http.Response, decoder RespDecoder) *Response
NewResponse create a new Response instance
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 RetryChecker ¶ added in v0.5.0
RetryChecker function type for checking if a request should be retried
type XmlDecoder ¶
type XmlDecoder struct {
}
XmlDecoder decodes http response body into a XML-tagged struct value.
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
cmd
|
|
|
gbench
command
|
|
|
greq
command
|
|
|
batch
Package batch provides utilities for batch processing HTTP requests
|
Package batch provides utilities for batch processing HTTP requests |
|
httpfile
Package httpfile provides HTTP request file(.http) parsing utilities.
|
Package httpfile provides HTTP request file(.http) parsing utilities. |