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 InitDefaultClient(setters ...ClientOption)
- type Client
- func (c *Client) Delete(url string, options ...Option) (*Response, error)
- func (c *Client) Get(url string, options ...Option) (*Response, error)
- func (c *Client) Patch(url string, options ...Option) (*Response, error)
- func (c *Client) Post(url string, options ...Option) (*Response, error)
- func (c *Client) Put(url string, options ...Option) (*Response, error)
- type ClientOption
- type Do
- type InterceptorFunc
- 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 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 Interceptor(interceptor InterceptorFunc) 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
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func InitDefaultClient ¶ added in v0.3.2
func InitDefaultClient(setters ...ClientOption)
InitDefaultClient initializes the default client with given options.
Types ¶
type Client ¶ added in v0.3.0
type Client struct {
// contains filtered or unexported fields
}
Client is an HTTP client which wraps around http.Client for elegant APIs and easy use.
func NewClient ¶ added in v0.3.2
func NewClient(setters ...ClientOption) *Client
NewClient creates a new client to serve HTTP requests.
func (*Client) Delete ¶ added in v0.3.2
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 (*Client) Get ¶ added in v0.3.2
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 (*Client) Patch ¶ added in v0.3.2
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.
type ClientOption ¶ added in v0.3.2
type ClientOption func(*Client)
ClientOption is the functional option type.
func WithInterceptor ¶ added in v0.3.0
func WithInterceptor(interceptor InterceptorFunc) ClientOption
WithInterceptor specifies an interceptor for client. You can use ChainInterceptors to chain multiple interceptors into one.
func WithTimeout ¶ added in v0.3.2
func WithTimeout(timeout time.Duration) ClientOption
WithTimeout specifies a time limit for each client request.
A Timeout of zero means no timeout. Default is zero.
func WithTransport ¶ added in v0.3.2
func WithTransport(transport http.RoundTripper) ClientOption
WithTransport specifies a transport for client.
type InterceptorFunc ¶ added in v0.3.2
InterceptorFunc 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.
func ChainInterceptors ¶ added in v0.3.2
func ChainInterceptors(interceptors ...InterceptorFunc) InterceptorFunc
ChainInterceptors chains multiple interceptors into one.
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 Data ¶ added in v0.0.4
Data sets data of request body. It also deduces Content-Type based on input data types:
1. auto deduce by http.DetectContentType: io.Reader, []byte 2. "application/json": struct, slice(except []byte), and map 3. "text/plain": others
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:
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 Interceptor ¶ added in v0.3.0
func Interceptor(interceptor InterceptorFunc) Option
Interceptor prepends an interceptor to environment interceptors for current request only.
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 sets the HTTP request context timeout by context.WithTimeout.
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
// dump
DumpRequestOut *string
DumpResponse *string
// interceptor
Interceptor InterceptorFunc
// contains filtered or unexported fields
}
Options defines all optional parameters for HTTP request.
type Request ¶ added in v0.3.0
Request is a wrapper of 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 "<nil>" if response is nil.
- It returns the empty string if the code is unknown.
e.g. "OK"