Documentation ¶
Index ¶
- Constants
- Variables
- func DefaultTransport() *http.Transport
- type AfterResponseHook
- type Backoff
- type BeforeRequestHook
- func SetDefaultBasicAuth(username string, password string) BeforeRequestHook
- func SetDefaultBearerToken(token string) BeforeRequestHook
- func SetDefaultContentType(contentType string) BeforeRequestHook
- func SetDefaultCookies(cookies Cookies) BeforeRequestHook
- func SetDefaultForm(form Form) BeforeRequestHook
- func SetDefaultHeaders(headers Headers) BeforeRequestHook
- func SetDefaultHost(host string) BeforeRequestHook
- func SetDefaultOrigin(origin string) BeforeRequestHook
- func SetDefaultQuery(query Params) BeforeRequestHook
- func SetDefaultReferer(referer string) BeforeRequestHook
- func SetDefaultRetry(maxAttempts int, backoff Backoff, triggers ...func(resp *Response) bool) BeforeRequestHook
- func SetDefaultUserAgent(userAgent string) BeforeRequestHook
- func SetReverseProxy(target string, publicPaths ...string) BeforeRequestHook
- type Client
- func (c *Client) AppendClientCerts(certs ...tls.Certificate) *Client
- func (c *Client) AppendRootCerts(pemFile string) *Client
- func (c *Client) Delete(url string, opts ...RequestOption) *Response
- func (c *Client) DisableProxy() *Client
- func (c *Client) DisableRedirect() *Client
- func (c *Client) DisableVerify() *Client
- func (c *Client) Do(req *Request) *Response
- func (c *Client) FilterCookie(url string, name string) (*http.Cookie, error)
- func (c *Client) FilterCookies(url string) ([]*http.Cookie, error)
- func (c *Client) Get(url string, opts ...RequestOption) *Response
- func (c *Client) Head(url string, opts ...RequestOption) *Response
- func (c *Client) OnAfterResponse(hooks ...AfterResponseHook) *Client
- func (c *Client) OnBeforeRequest(hooks ...BeforeRequestHook) *Client
- func (c *Client) Patch(url string, opts ...RequestOption) *Response
- func (c *Client) Post(url string, opts ...RequestOption) *Response
- func (c *Client) Put(url string, opts ...RequestOption) *Response
- func (c *Client) Send(method string, url string, opts ...RequestOption) *Response
- func (c *Client) SetCookieJar(jar http.CookieJar) *Client
- func (c *Client) SetCookies(url string, cookies ...*http.Cookie) *Client
- func (c *Client) SetProxy(proxy func(*http.Request) (*neturl.URL, error)) *Client
- func (c *Client) SetProxyFromURL(url string) *Client
- func (c *Client) SetRateLimiter(limiter *rate.Limiter, urlPatterns ...*regexp.Regexp) *Client
- func (c *Client) SetRedirect(policy func(req *http.Request, via []*http.Request) error) *Client
- func (c *Client) SetTLSClientConfig(config *tls.Config) *Client
- func (c *Client) SetTimeout(timeout time.Duration) *Client
- func (c *Client) SetTransport(transport http.RoundTripper) *Client
- type Cookies
- func (c Cookies) Clone() Cookies
- func (c Cookies) Decode() []*http.Cookie
- func (c Cookies) Del(key string)
- func (c Cookies) Get(key string) *http.Cookie
- func (c Cookies) Has(key string) bool
- func (c Cookies) Merge(c2 Cookies)
- func (c Cookies) Set(key string, value string)
- func (c Cookies) SetDefault(key string, value string)
- func (c Cookies) Update(c2 Cookies)
- type Error
- type File
- type Files
- type Form
- type FormFile
- type H
- func (h H) Decode(output interface{}) error
- func (h H) Get(key string) interface{}
- func (h H) GetBool(key string) bool
- func (h H) GetBoolDefault(key string, defaultValue bool) bool
- func (h H) GetBoolSlice(key string) []bool
- func (h H) GetH(key string) H
- func (h H) GetHSlice(key string) []H
- func (h H) GetNumber(key string) Number
- func (h H) GetNumberDefault(key string, defaultValue Number) Number
- func (h H) GetNumberSlice(key string) []Number
- func (h H) GetSlice(key string) []interface{}
- func (h H) GetString(key string) string
- func (h H) GetStringDefault(key string, defaultValue string) string
- func (h H) GetStringSlice(key string) []string
- func (h H) Has(key string) bool
- func (h H) String() string
- type Headers
- type Number
- func (n Number) Float32() float32
- func (n Number) Float64() float64
- func (n Number) Int() int
- func (n Number) Int16() int16
- func (n Number) Int32() int32
- func (n Number) Int64() int64
- func (n Number) Int8() int8
- func (n Number) String() string
- func (n Number) Uint() uint
- func (n Number) Uint16() uint16
- func (n Number) Uint32() uint32
- func (n Number) Uint64() uint64
- func (n Number) Uint8() uint8
- type Params
- type Request
- func (req *Request) Decode() *http.Request
- func (req *Request) Dump(withBody bool) ([]byte, error)
- func (req *Request) ExportCURLCommand() (string, error)
- func (req *Request) SetBasicAuth(username string, password string) *Request
- func (req *Request) SetBearerToken(token string) *Request
- func (req *Request) SetBody(body io.Reader) *Request
- func (req *Request) SetContent(content []byte) *Request
- func (req *Request) SetContentType(contentType string) *Request
- func (req *Request) SetContext(ctx context.Context) *Request
- func (req *Request) SetCookies(cookies Cookies) *Request
- func (req *Request) SetForm(form Form) *Request
- func (req *Request) SetHeaders(headers Headers) *Request
- func (req *Request) SetHost(host string) *Request
- func (req *Request) SetJSON(data interface{}, escapeHTML bool) error
- func (req *Request) SetMultipart(files Files, form Form) *Request
- func (req *Request) SetOrigin(origin string) *Request
- func (req *Request) SetQuery(query Params) *Request
- func (req *Request) SetReferer(referer string) *Request
- func (req *Request) SetRetry(maxAttempts int, backoff Backoff, triggers ...func(resp *Response) bool) *Request
- func (req *Request) SetText(text string) *Request
- func (req *Request) SetUserAgent(userAgent string) *Request
- func (req *Request) SetXML(data interface{}) error
- type RequestOption
- func WithBasicAuth(username string, password string) RequestOption
- func WithBearerToken(token string) RequestOption
- func WithBody(body io.Reader) RequestOption
- func WithContent(content []byte) RequestOption
- func WithContentType(contentType string) RequestOption
- func WithContext(ctx context.Context) RequestOption
- func WithCookies(cookies Cookies) RequestOption
- func WithForm(form Form) RequestOption
- func WithHeaders(headers Headers) RequestOption
- func WithHost(host string) RequestOption
- func WithJSON(data interface{}, escapeHTML bool) RequestOption
- func WithMultipart(files Files, form Form) RequestOption
- func WithOrigin(origin string) RequestOption
- func WithQuery(query Params) RequestOption
- func WithReferer(referer string) RequestOption
- func WithRetry(maxAttempts int, backoff Backoff, triggers ...func(resp *Response) bool) RequestOption
- func WithText(text string) RequestOption
- func WithUserAgent(userAgent string) RequestOption
- func WithXML(data interface{}) RequestOption
- type Response
- func Delete(url string, opts ...RequestOption) *Response
- func Get(url string, opts ...RequestOption) *Response
- func Head(url string, opts ...RequestOption) *Response
- func Patch(url string, opts ...RequestOption) *Response
- func Post(url string, opts ...RequestOption) *Response
- func Put(url string, opts ...RequestOption) *Response
- func Send(method string, url string, opts ...RequestOption) *Response
- func (resp *Response) Content() ([]byte, error)
- func (resp *Response) Cookie(name string) (*http.Cookie, error)
- func (resp *Response) Cookies() ([]*http.Cookie, error)
- func (resp *Response) Dump(withBody bool) ([]byte, error)
- func (resp *Response) EnsureStatus(code int) *Response
- func (resp *Response) EnsureStatus2xx() *Response
- func (resp *Response) EnsureStatusOk() *Response
- func (resp *Response) Error() error
- func (resp *Response) H() (H, error)
- func (resp *Response) JSON(v interface{}) error
- func (resp *Response) Prefetch() *Response
- func (resp *Response) Raw() (*http.Response, error)
- func (resp *Response) Save(filename string, perm os.FileMode) error
- func (resp *Response) Text(e ...encoding.Encoding) (string, error)
- func (resp *Response) Verbose(w io.Writer, withBody bool) (err error)
- func (resp *Response) XML(v interface{}) error
- type Retry
- type Values
- func (v Values) Decode(canonicalHeaderKey bool) map[string][]string
- func (v Values) Del(key string)
- func (v Values) Get(key string) []string
- func (v Values) Has(key string) bool
- func (v Values) Marshal() string
- func (v Values) Merge(v2 Values)
- func (v Values) Set(key string, value interface{})
- func (v Values) SetDefault(key string, value interface{})
- func (v Values) URLEncode(escaped bool) string
- func (v Values) Update(v2 Values)
Constants ¶
const ( // MethodGet represents the GET method for HTTP. MethodGet = "GET" // MethodHead represents the HEAD method for HTTP. MethodHead = "HEAD" // MethodPost represents the POST method for HTTP. MethodPost = "POST" // MethodPut represents the PUT method for HTTP. MethodPut = "PUT" // MethodPatch represents the PATCH method for HTTP. MethodPatch = "PATCH" // MethodDelete represents the DELETE method for HTTP. MethodDelete = "DELETE" // MethodConnect represents the CONNECT method for HTTP. MethodConnect = "CONNECT" // MethodOptions represents the OPTIONS method for HTTP. MethodOptions = "OPTIONS" // MethodTrace represents the TRACE method for HTTP. MethodTrace = "TRACE" )
const (
// Version of sreq.
Version = "0.9.11"
)
Variables ¶
var ( // ErrNilCookieJar can be used when the cookie jar is nil. ErrNilCookieJar = errors.New("sreq: nil cookie jar") // ErrNoCookie can be used when a cookie not found in the HTTP response or cookie jar. ErrNoCookie = errors.New("sreq: named cookie not present") )
var ( // DefaultBackoff specifies an exponential backoff with jitter // whose minWaitTime is 1s and maxWaitTime is 30s. DefaultBackoff = NewExponentialBackoff(1*time.Second, 30*time.Second, true) )
var ( // GlobalClient is a sreq Client used by the global functions such as Get, Post, etc. GlobalClient = New() )
Functions ¶
func DefaultTransport ¶
DefaultTransport returns a preset HTTP transport. It's a clone of http.DefaultTransport indeed.
Types ¶
type AfterResponseHook ¶ added in v0.8.16
AfterResponseHook specifies an after response hook. If the returned error isn't nil, sreq will consider resp as a bad response.
type Backoff ¶ added in v0.8.15
type Backoff interface { // WaitTime returns the wait time to sleep before retrying request. WaitTime(attemptNum int, resp *Response) time.Duration }
Backoff is the interface that specifies a backoff strategy for handling retries.
func NewExponentialBackoff ¶ added in v0.9.10
NewExponentialBackoff provides a callback for the retry policy which will perform exponential backoff with jitter based on the attempt number and limited by the provided minimum and maximum durations. See: https://aws.amazon.com/cn/blogs/architecture/exponential-backoff-and-jitter/
type BeforeRequestHook ¶ added in v0.8.16
BeforeRequestHook is alike to RequestOption, but for specifying a before request hook. Return a non-nil error to prevent requests.
func SetDefaultBasicAuth ¶ added in v0.8.17
func SetDefaultBasicAuth(username string, password string) BeforeRequestHook
SetDefaultBasicAuth is a before request hook to set client level basic authentication, can be overwrite by request level option.
func SetDefaultBearerToken ¶ added in v0.8.17
func SetDefaultBearerToken(token string) BeforeRequestHook
SetDefaultBearerToken is a before request hook to set client level bearer token, can be overwrite by request level option.
func SetDefaultContentType ¶ added in v0.8.17
func SetDefaultContentType(contentType string) BeforeRequestHook
SetDefaultContentType is a before request hook to set client level Content-Type header value, can be overwrite by request level option.
func SetDefaultCookies ¶ added in v0.8.17
func SetDefaultCookies(cookies Cookies) BeforeRequestHook
SetDefaultCookies is a before request hook to set client level cookies, can be overwrite by request level option.
func SetDefaultForm ¶ added in v0.8.17
func SetDefaultForm(form Form) BeforeRequestHook
SetDefaultForm is a before request hook to set client level form payload, can be overwrite by request level option.
func SetDefaultHeaders ¶ added in v0.8.17
func SetDefaultHeaders(headers Headers) BeforeRequestHook
SetDefaultHeaders is a before request hook to set client level headers, can be overwrite by request level option.
func SetDefaultHost ¶ added in v0.8.17
func SetDefaultHost(host string) BeforeRequestHook
SetDefaultHost is a before request hook set client level Host header value, can be overwrite by request level option.
func SetDefaultOrigin ¶ added in v0.9.9
func SetDefaultOrigin(origin string) BeforeRequestHook
SetDefaultOrigin is a before request hook to set client level Origin header value, can be overwrite by request level option.
func SetDefaultQuery ¶ added in v0.8.17
func SetDefaultQuery(query Params) BeforeRequestHook
SetDefaultQuery is a before request hook to set client level query parameters, can be overwrite by request level option.
func SetDefaultReferer ¶ added in v0.8.17
func SetDefaultReferer(referer string) BeforeRequestHook
SetDefaultReferer is a before request hook to set client level Referer header value, can be overwrite by request level option.
func SetDefaultRetry ¶ added in v0.8.17
func SetDefaultRetry(maxAttempts int, backoff Backoff, triggers ...func(resp *Response) bool) BeforeRequestHook
SetDefaultRetry is a before request hook to set client level retry policy, can be overwrite by request level option.
func SetDefaultUserAgent ¶ added in v0.8.17
func SetDefaultUserAgent(userAgent string) BeforeRequestHook
SetDefaultUserAgent is a before request hook to set client level User-Agent header value, can be overwrite by request level option.
func SetReverseProxy ¶ added in v0.9.9
func SetReverseProxy(target string, publicPaths ...string) BeforeRequestHook
SetReverseProxy is a before request hook to set reverse proxy for HTTP requests.
type Client ¶
Client wraps the raw HTTP client. Do not modify the client across Goroutines! You should reuse it as possible after initialized.
func NewWithHTTPClient ¶ added in v0.9.10
NewWithHTTPClient returns a new Client given an *http.Client.
func NewWithSession ¶ added in v0.9.10
func NewWithSession() *Client
NewWithSession is like New but with session support.
func (*Client) AppendClientCerts ¶ added in v0.7.11
func (c *Client) AppendClientCerts(certs ...tls.Certificate) *Client
AppendClientCerts appends client certificates to the HTTP client.
func (*Client) AppendRootCerts ¶ added in v0.7.12
AppendRootCerts appends root certificates from a pem file to the HTTP client. If there is an error while reading pemFile, it will panic.
func (*Client) Delete ¶
func (c *Client) Delete(url string, opts ...RequestOption) *Response
Delete makes a DELETE HTTP request.
func (*Client) DisableProxy ¶
DisableProxy makes the HTTP client not use proxy.
func (*Client) DisableRedirect ¶
DisableRedirect makes the HTTP client not follow redirects.
func (*Client) DisableVerify ¶
DisableVerify makes the HTTP client not verify the server's TLS certificate.
func (*Client) FilterCookie ¶
FilterCookie returns the named cookie to send in a request for the given URL from cookie jar.
func (*Client) FilterCookies ¶
FilterCookies returns the cookies to send in a request for the given URL from cookie jar.
func (*Client) Get ¶
func (c *Client) Get(url string, opts ...RequestOption) *Response
Get makes a GET HTTP request.
func (*Client) Head ¶
func (c *Client) Head(url string, opts ...RequestOption) *Response
Head makes a HEAD HTTP request.
func (*Client) OnAfterResponse ¶ added in v0.8.16
func (c *Client) OnAfterResponse(hooks ...AfterResponseHook) *Client
OnAfterResponse appends response hooks into the after response chain.
func (*Client) OnBeforeRequest ¶ added in v0.8.16
func (c *Client) OnBeforeRequest(hooks ...BeforeRequestHook) *Client
OnBeforeRequest appends request hooks into the before request chain.
func (*Client) Patch ¶
func (c *Client) Patch(url string, opts ...RequestOption) *Response
Patch makes a PATCH HTTP request.
func (*Client) Post ¶
func (c *Client) Post(url string, opts ...RequestOption) *Response
Post makes a POST HTTP request.
func (*Client) Put ¶
func (c *Client) Put(url string, opts ...RequestOption) *Response
Put makes a PUT HTTP request.
func (*Client) Send ¶
func (c *Client) Send(method string, url string, opts ...RequestOption) *Response
Send makes an HTTP request using a specified method.
func (*Client) SetCookieJar ¶
SetCookieJar sets cookie jar of the HTTP client.
func (*Client) SetCookies ¶
SetCookies sets cookies to cookie jar for the given URL. If the cookie jar is nil, it will panic.
func (*Client) SetProxyFromURL ¶
SetProxyFromURL sets proxy of the HTTP client from a URL.
func (*Client) SetRateLimiter ¶ added in v0.9.10
SetRateLimiter specifies a rate-limiter for c to handle outbound requests. If one or more urlPatterns are specified, only the URL matches one of the patterns will be limited.
func (*Client) SetRedirect ¶
SetRedirect sets policy of the HTTP client for handling redirects.
func (*Client) SetTLSClientConfig ¶
SetTLSClientConfig sets TLS configuration of the HTTP client.
func (*Client) SetTimeout ¶
SetTimeout sets timeout of the HTTP client, default is 120s.
func (*Client) SetTransport ¶
func (c *Client) SetTransport(transport http.RoundTripper) *Client
SetTransport sets transport of the HTTP client.
type Cookies ¶ added in v0.8.8
Cookies is a shortcut for map[string]string, used for request cookies.
func (Cookies) Decode ¶ added in v0.8.8
Decode translates c and returns the equivalent request cookies.
func (Cookies) SetDefault ¶ added in v0.8.8
SetDefault sets the key to value if the value not exists.
type Error ¶ added in v0.9.2
Error records an error with more details to makes it more readable.
type File ¶ added in v0.6.0
File specifies a file to upload, it implements FormFile interface. Note: To upload a file its filename must be specified, if not, sreq will use "file" as default. If you don't specify the mime, sreq will detect automatically using http.DetectContentType.
func MustOpen ¶ added in v0.3.0
MustOpen opens the named file and returns a *File instance. If there is an error, it will panic.
func (*File) Filename ¶ added in v0.6.0
Filename implements FormFile interface, returns the filename of f.
func (*File) SetFilename ¶ added in v0.6.0
SetFilename specifies the filename of f.
type Files ¶
Files maps a string key to a FormFile type value, used for form files of multipart payload.
type FormFile ¶ added in v0.9.9
type FormFile interface { io.ReadCloser Filename() string MIME() string }
FormFile is the interface that specifies a form file of multipart payload.
type H ¶ added in v0.7.0
type H map[string]interface{}
H is a shortcut for map[string]interface{}, used for JSON unmarshalling. Do not use it for other purposes!
func (H) Decode ¶ added in v0.8.7
Decode encodes h to JSON and then decodes to the output structure. output must be a pointer.
func (H) GetBool ¶ added in v0.7.0
GetBool gets the bool value associated with key. The zero value is returned if the key not exists.
func (H) GetBoolDefault ¶ added in v0.8.0
GetBoolDefault gets the bool value associated with key. The defaultValue is returned if the key not exists.
func (H) GetBoolSlice ¶ added in v0.7.0
GetBoolSlice gets the []bool value associated with key.
func (H) GetNumber ¶ added in v0.8.7
GetNumber gets the Number value associated with key. The zero value is returned if the key not exists.
func (H) GetNumberDefault ¶ added in v0.8.7
GetNumberDefault gets the Number value associated with key. The defaultValue is returned if the key not exists.
func (H) GetNumberSlice ¶ added in v0.8.7
GetNumberSlice gets the []Number value associated with key.
func (H) GetString ¶ added in v0.7.0
GetString gets the string value associated with key. The zero value is returned if the key not exists.
func (H) GetStringDefault ¶ added in v0.8.0
GetStringDefault gets the string value associated with key. The defaultValue is returned if the key not exists.
func (H) GetStringSlice ¶ added in v0.7.0
GetStringSlice gets the []string value associated with key.
type Number ¶ added in v0.8.7
type Number float64
Number is a shortcut for float64.
type Params ¶
type Params = Values
Params is an alias of Values, used for for request query parameters.
type Request ¶
Request wraps the raw HTTP request.
func NewRequest ¶
func NewRequest(method string, url string, opts ...RequestOption) (*Request, error)
NewRequest returns a new Request given a method, URL and optional request options.
func (*Request) Decode ¶ added in v0.9.7
Decode translates req and returns the equivalent raw HTTP request.
func (*Request) ExportCURLCommand ¶ added in v0.9.10
ExportCURLCommand converts req to CURL command line.
func (*Request) SetBasicAuth ¶
SetBasicAuth sets basic authentication for the HTTP request.
func (*Request) SetBearerToken ¶
SetBearerToken sets bearer token for the HTTP request.
func (*Request) SetContent ¶
SetContent sets bytes payload for the HTTP request.
func (*Request) SetContentType ¶
SetContentType sets Content-Type header value for the HTTP request.
func (*Request) SetContext ¶
SetContext sets context for the HTTP request.
func (*Request) SetCookies ¶
SetCookies sets cookies for the HTTP request.
func (*Request) SetHeaders ¶
SetHeaders sets headers for the HTTP request.
func (*Request) SetMultipart ¶ added in v0.3.0
SetMultipart sets multipart payload for the HTTP request.
func (*Request) SetOrigin ¶ added in v0.9.9
SetOrigin sets Origin header value for the HTTP request.
func (*Request) SetReferer ¶
SetReferer sets Referer header value for the HTTP request.
func (*Request) SetRetry ¶
func (req *Request) SetRetry(maxAttempts int, backoff Backoff, triggers ...func(resp *Response) bool) *Request
SetRetry specifies the retry policy for handling retries. The backoff is optional, default is DefaultBackoff.
func (*Request) SetUserAgent ¶
SetUserAgent sets User-Agent header value for the HTTP request.
type RequestOption ¶
RequestOption provides a convenient way to setup Request.
func WithBasicAuth ¶
func WithBasicAuth(username string, password string) RequestOption
WithBasicAuth is a request option to set basic authentication for the HTTP request.
func WithBearerToken ¶
func WithBearerToken(token string) RequestOption
WithBearerToken is a request option to set bearer token for the HTTP request.
func WithBody ¶
func WithBody(body io.Reader) RequestOption
WithBody is a request option to set body for the HTTP request.
func WithContent ¶
func WithContent(content []byte) RequestOption
WithContent is a request option to set bytes payload for the HTTP request.
func WithContentType ¶
func WithContentType(contentType string) RequestOption
WithContentType is a request option to set Content-Type header value for the HTTP request.
func WithContext ¶
func WithContext(ctx context.Context) RequestOption
WithContext is a request option to set context for the HTTP request.
func WithCookies ¶
func WithCookies(cookies Cookies) RequestOption
WithCookies is a request option to set cookies for the HTTP request.
func WithForm ¶
func WithForm(form Form) RequestOption
WithForm is a request option to set form payload for the HTTP request.
func WithHeaders ¶
func WithHeaders(headers Headers) RequestOption
WithHeaders is a request option to set headers for the HTTP request.
func WithHost ¶
func WithHost(host string) RequestOption
WithHost is a request option to set Host header value for the HTTP request.
func WithJSON ¶
func WithJSON(data interface{}, escapeHTML bool) RequestOption
WithJSON is a request option to set JSON payload for the HTTP request.
func WithMultipart ¶ added in v0.3.0
func WithMultipart(files Files, form Form) RequestOption
WithMultipart is a request option sets multipart payload for the HTTP request.
func WithOrigin ¶ added in v0.9.9
func WithOrigin(origin string) RequestOption
WithOrigin is a request option to set Origin header value for the HTTP request.
func WithQuery ¶
func WithQuery(query Params) RequestOption
WithQuery is a request option to set query parameters for the HTTP request.
func WithReferer ¶
func WithReferer(referer string) RequestOption
WithReferer is a request option to set Referer header value for the HTTP request.
func WithRetry ¶
func WithRetry(maxAttempts int, backoff Backoff, triggers ...func(resp *Response) bool) RequestOption
WithRetry is a request option to specify the retry policy for handling retries.
func WithText ¶
func WithText(text string) RequestOption
WithText is a request option to set plain text payload for the HTTP request.
func WithUserAgent ¶
func WithUserAgent(userAgent string) RequestOption
WithUserAgent is a request option to set User-Agent header value for the HTTP request.
func WithXML ¶ added in v0.6.0
func WithXML(data interface{}) RequestOption
WithXML is a request option to set XML payload for the HTTP request.
type Response ¶
Response wraps the raw HTTP response.
func Delete ¶
func Delete(url string, opts ...RequestOption) *Response
Delete makes a DELETE HTTP request.
func Patch ¶
func Patch(url string, opts ...RequestOption) *Response
Patch makes a PATCH HTTP request.
func Send ¶
func Send(method string, url string, opts ...RequestOption) *Response
Send makes an HTTP request using a specified method.
func (*Response) EnsureStatus ¶
EnsureStatus ensures the HTTP response's status code must be code.
func (*Response) EnsureStatus2xx ¶
EnsureStatus2xx ensures the HTTP response's status code must be 2xx.
func (*Response) EnsureStatusOk ¶
EnsureStatusOk ensures the HTTP response's status code must be 200.
func (*Response) H ¶ added in v0.7.0
H decodes the HTTP response body and unmarshals its JSON-encoded data into an H instance.
func (*Response) JSON ¶
JSON decodes the HTTP response body and unmarshals its JSON-encoded data into v. v must be a pointer.
func (*Response) Prefetch ¶ added in v0.9.9
Prefetch reads from the HTTP response body until an error or EOF and keeps the data in memory for reuse.
func (*Response) Text ¶
Text decodes the HTTP response body and returns the text representation of its raw data given an optional charset encoding.
type Retry ¶ added in v0.8.17
type Retry struct { // MaxAttempts specifies the max attempts of the retry policy, 1 means no retries. MaxAttempts int // Backoff specifies the backoff of the retry policy. It is called // after a failing request to determine the amount of time // that should pass before trying again. Backoff Backoff // Triggers specifies a group of triggers for handling retries. It is called // following each request with the response values returned by Client. // If the triggers not specified, default is the error of resp isn't nil. // Otherwise, the Client will only retry the request when the response meets one of the triggers. Triggers []func(resp *Response) bool }
Retry specifies the retry policy for handling retries.
type Values ¶ added in v0.6.0
type Values map[string]interface{}
Values maps a string key to an interface{} type value, When it's used for request query parameters, form data or headers, besides string and []string, its value also supports bool and number, sreq will convert to string automatically.
func (Values) Decode ¶ added in v0.8.8
Decode translates v and returns the equivalent request query parameters, form data or headers.
func (Values) Get ¶ added in v0.6.0
Get gets the equivalent request query parameter, form data or header value associated with key.
func (Values) SetDefault ¶ added in v0.8.1
SetDefault sets the key to value if the value not exists.