Documentation
¶
Overview ¶
Package requests is an elegant and simple HTTP library for golang, built for human beings.
This package mimics the implementation of the classic Python package Requests(https://requests.readthedocs.io/)
Index ¶
- func SetEnvTimeout(timeout time.Duration)
- func WithInterceptor(interceptors ...Interceptor)
- type Client
- type Do
- type Interceptor
- type Option
- func BasicAuth(username, password string) Option
- func Body(body io.Reader) Option
- func Context(ctx context.Context) Option
- func Data(data any) Option
- func DisableKeepAlives() Option
- func Dump(req, resp *string) Option
- func Files(files map[string]*os.File) Option
- func Form[T map[string]string | url.Values](params T) Option
- func FormPairs(kv ...string) Option
- func HeaderPairs(kv ...string) Option
- func Headers[T map[string]string | http.Header](headers T) Option
- func JSON(v any) Option
- func ParamPairs(kv ...string) Option
- func Params[T map[string]string | url.Values](params T) Option
- func Timeout(timeout time.Duration) Option
- func ToJSON(v any) Option
- func ToText(v *string) Option
- type Options
- type Request
- type Response
- func Delete(url string, options ...Option) (*Response, error)
- func Get(url string, options ...Option) (*Response, error)
- func Patch(url string, options ...Option) (*Response, error)
- func Post(url string, options ...Option) (*Response, error)
- func Put(url string, options ...Option) (*Response, error)
- func (r *Response) Bytes() []byte
- func (r *Response) Cookies() map[string]*http.Cookie
- func (r *Response) Headers() http.Header
- func (r *Response) JSON(v any) error
- func (r *Response) Method() string
- func (r *Response) StatusCode() int
- func (r *Response) StatusText() string
- func (r *Response) Text() string
- func (r *Response) URL() string
- type Stats
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func SetEnvTimeout ¶ added in v0.0.7
SetEnvTimeout sets the default timeout for each HTTP request at the environment level.
func WithInterceptor ¶ added in v0.3.0
func WithInterceptor(interceptors ...Interceptor)
WithInterceptor specifies the interceptor for each HTTP request.
Types ¶
type Interceptor ¶ added in v0.3.0
Interceptor provides a hook to intercept the execution of an HTTP request invocation. When an interceptor(s) is set, requests delegates all HTTP client invocations to the interceptor, and it is the responsibility of the interceptor to call do to complete the processing of the HTTP request.
type Option ¶
type Option func(*Options)
Option is the functional option type.
func Context ¶ added in v0.3.0
Context sets the HTTP request context.
For outgoing client request, the context controls the entire lifetime of a request and its response: obtaining a connection, sending the request, and reading the response headers and body.
func DisableKeepAlives ¶ added in v0.1.2
func DisableKeepAlives() Option
DisableKeepAlives, if true, disables HTTP keep-alives and will only use the connection to the server for a single HTTP request.
This is unrelated to the similarly named TCP keep-alives.
func Dump ¶ added in v0.2.2
Dump dumps outgoing client request and response to the corresponding input param (req or resp) if not nil.
Refer: - https://pkg.go.dev/net/http/httputil#DumpRequestOut - https://pkg.go.dev/net/http/httputil#DumpResponse
func Files ¶ added in v0.1.0
Files sets files to a map of (field, fileHandler). It also sets the Content-Type as "multipart/form-data".
func Form ¶
Form sets the given form values into the request body. It also sets the Content-Type as "application/x-www-form-urlencoded". Two types are supported:
Type 1: map[string]string ¶
map[string]string( "key1", "val1", "key2", "val2", )
Type 2: url.Values ¶
url.Values( "key1", []string{"val1", "val1-2"}, "key2", "val2", )
func FormPairs ¶
FormPairs sets form values by the mapping of key-value pairs. It panics if len(kv) is odd.
Values with the same key will be merged into a list:
FormPairs( "key1", "val1", "key1", "val1-2", // "key1" will have map value []string{"val1", "val1-2"} "key2", "val2", )
func HeaderPairs ¶
HeaderPairs sets HTTP headers formed by the mapping of key-value pairs. The keys should be in canonical form, as returned by http.CanonicalHeaderKey. It panics if len(kv) is odd.
Values with the same key will be merged into a list:
HeaderPairs( "Key1", "val1", "Key1", "val1-2", // "Key1" will have map value []string{"val1", "val1-2"} "Key2", "val2", )
func Headers ¶
Headers sets the HTTP headers. The keys should be in canonical form, as returned by http.CanonicalHeaderKey. Two types are supported:
Type 1: map[string]string ¶
map[string]string( "Header-Key1", "val1", "Header-Key2", "val2", )
Type 2: http.Header ¶
http.Header( "Header-Key1", []string{"val1", "val1-2"}, "Header-Key2", "val2", )
func JSON ¶
JSON marshals the given struct as JSON into the request body. It also sets the Content-Type as "application/json".
func ParamPairs ¶
ParamPairs sets the query parameters formed by the mapping of key-value pairs. It panics if len(kv) is odd.
Values with the same key will be merged into a list:
ParamPairs( "key1", "val1", "key1", "val1-2", // "key1" will have map value []string{"val1", "val1-2"} "key2", "val2", )
func Params ¶
Params sets the given query parameters into the URL query string. Two types are supported:
Type 1: map[string]string ¶
map[string]string( "key1", "val1", "key2", "val2", )
Type 2: url.Values ¶
url.Values( "key1", []string{"val1", "val1-2"}, "key2", "val2", )
func Timeout ¶ added in v0.0.7
Timeout specifies a time limit for requests made by this Client. The timeout includes connection time, any redirects, and reading the response body. The timer remains running after Get, Head, Post, or Do return and will interrupt reading of the Response.Body.
A Timeout of zero means no timeout. Default is 60s.
type Options ¶
type Options struct { Headers http.Header Params url.Values Body io.Reader // different request body types Data any Form url.Values JSON any Files map[string]*os.File // different response body types ToText *string ToJSON any // auth AuthInfo *auth.AuthInfo // request timeout Timeout time.Duration DisableKeepAlives bool // dump DumpRequestOut *string DumpResponse *string // contains filtered or unexported fields }
Options defines all optional parameters for HTTP request.
type Response ¶
Response is a wrapper of http.Response.
func Delete ¶
Delete sends an HTTP request with DELETE method.
On error, any Response can be ignored. A non-nil Response with a non-nil error only occurs when Response.StatusCode() is not 2xx.
func Get ¶
Get sends an HTTP request with GET method.
On error, any Response can be ignored. A non-nil Response with a non-nil error only occurs when Response.StatusCode() is not 2xx.
func Patch ¶ added in v0.1.0
Patch sends an HTTP request with PATCH method.
On error, any Response can be ignored. A non-nil Response with a non-nil error only occurs when Response.StatusCode() is not 2xx.
func Put ¶
Put sends an HTTP request with PUT method.
On error, any Response can be ignored. A non-nil Response with a non-nil error only occurs when Response.StatusCode() is not 2xx.
func (*Response) Cookies ¶ added in v0.0.9
Cookies parses and returns the cookies set in the Set-Cookie headers.
func (*Response) Headers ¶ added in v0.0.9
Headers maps header keys to values. If the response had multiple headers with the same key, they may be concatenated, with comma delimiters.
func (*Response) StatusCode ¶
StatusCode returns status code of HTTP response.
NOTE: It returns -1 if response is nil.
func (*Response) StatusText ¶ added in v0.3.0
StatusText returns a text for the HTTP status code.
NOTE:
- It returns "Response is nil" if response is nil.
- It returns the empty string if the code is unknown.
e.g. "OK"