Documentation
¶
Index ¶
- func Delete(url string, opts ...RequestOption) (resp *http.Response, err error)
- func Get(url string, opts ...RequestOption) (resp *http.Response, err error)
- func Head(url string, opts ...RequestOption) (resp *http.Response, err error)
- func Patch(url string, contentType string, body io.Reader, opts ...RequestOption) (resp *http.Response, err error)
- func PatchJSON(url string, body any, opts ...RequestOption) (resp *http.Response, err error)
- func Post(url string, contentType string, body io.Reader, opts ...RequestOption) (resp *http.Response, err error)
- func PostForm(url string, data url.Values, opts ...RequestOption) (resp *http.Response, err error)
- func PostFormFiles(url string, data url.Values, files []*UploadFile, opts ...RequestOption) (resp *http.Response, err error)
- func PostJSON(url string, body any, opts ...RequestOption) (resp *http.Response, err error)
- func Put(url string, contentType string, body io.Reader, opts ...RequestOption) (resp *http.Response, err error)
- func PutJSON(url string, body any, opts ...RequestOption) (resp *http.Response, err error)
- func ReadAll(resp *http.Response) (b []byte, err error)
- func ReadAnyJSON(resp *http.Response, v any) (err error)
- func ReadJSON[T any](resp *http.Response) (T, error)
- func ReadString(resp *http.Response) (s string, err error)
- func SaveFile(resp *http.Response, name string) (written int64, err error)
- func WithContentType(contentType string) func(*RequestOptions)
- func WithDialTimeout(dialTimeout time.Duration) func(*ClientOptions)
- func WithHeaders(headers http.Header) func(*RequestOptions)
- func WithKeepAliveTimeout(keepAliveTimeout time.Duration) func(*ClientOptions)
- func WithProxy(proxy string) func(*ClientOptions)
- func WithReferer(referer string) func(*RequestOptions)
- func WithTLSHandshakeTimeout(tlsHandshakeTimeout time.Duration) func(*ClientOptions)
- func WithUserAgent(userAgent string) func(*ClientOptions)
- type Client
- func (c *Client) Close()
- func (c *Client) Delete(url string, opts ...RequestOption) (resp *http.Response, err error)
- func (c *Client) Do(req *http.Request, opts ...RequestOption) (resp *http.Response, err error)
- func (c *Client) Get(url string, opts ...RequestOption) (resp *http.Response, err error)
- func (c *Client) Head(url string, opts ...RequestOption) (resp *http.Response, err error)
- func (c *Client) Patch(url string, contentType string, body io.Reader, opts ...RequestOption) (resp *http.Response, err error)
- func (c *Client) PatchJSON(url string, body any, opts ...RequestOption) (resp *http.Response, err error)
- func (c *Client) Post(url string, contentType string, body io.Reader, opts ...RequestOption) (resp *http.Response, err error)
- func (c *Client) PostForm(url string, data url.Values, opts ...RequestOption) (resp *http.Response, err error)
- func (c *Client) PostFormFiles(url string, data url.Values, files []*UploadFile, opts ...RequestOption) (resp *http.Response, err error)
- func (c *Client) PostJSON(url string, body any, opts ...RequestOption) (resp *http.Response, err error)
- func (c *Client) Put(url string, contentType string, body io.Reader, opts ...RequestOption) (resp *http.Response, err error)
- func (c *Client) PutJSON(url string, body any, opts ...RequestOption) (resp *http.Response, err error)
- type ClientOption
- type ClientOptions
- type RequestOption
- type RequestOptions
- type UploadFile
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Delete ¶
func Delete(url string, opts ...RequestOption) (resp *http.Response, err error)
Delete issues a DELETE to the specified URL.
When err is nil, resp always contains a non-nil resp.Body. Caller should close resp.Body when done reading from it.
func Get ¶
func Get(url string, opts ...RequestOption) (resp *http.Response, err error)
Get issues a GET to the specified URL.
When err is nil, resp always contains a non-nil resp.Body. Caller should close resp.Body when done reading from it.
func Head ¶
func Head(url string, opts ...RequestOption) (resp *http.Response, err error)
Head issues a HEAD to the specified URL.
When err is nil, resp always contains a non-nil resp.Body. Caller should close resp.Body when done reading from it.
func Patch ¶
func Patch(url string, contentType string, body io.Reader, opts ...RequestOption) (resp *http.Response, err error)
Patch issues a PATCH to the specified URL.
When err is nil, resp always contains a non-nil resp.Body. Caller should close resp.Body when done reading from it.
func PatchJSON ¶
PatchJSON issues a PATCH to the specified URL with the given body as JSON. values JSON as the request body.
The Content-Type header is set to application/json.
When err is nil, resp always contains a non-nil resp.Body. Caller should close resp.Body when done reading from it.
func Post ¶
func Post(url string, contentType string, body io.Reader, opts ...RequestOption) (resp *http.Response, err error)
Post issues a POST to the specified URL.
When err is nil, resp always contains a non-nil resp.Body. Caller should close resp.Body when done reading from it.
func PostForm ¶
PostForm issues a POST to the specified URL, with data's keys and values URL-encoded as the request body.
The Content-Type header is set to application/x-www-form-urlencoded.
When err is nil, resp always contains a non-nil resp.Body. Caller should close resp.Body when done reading from it.
func PostFormFiles ¶
func PostFormFiles(url string, data url.Values, files []*UploadFile, opts ...RequestOption) (resp *http.Response, err error)
PostFormFiles issues a POST to the specified URL, with data's keys and values URL-encoded as the request body, and files as multipart form.
The Content-Type header is set to multipart/form-data.
When err is nil, resp always contains a non-nil resp.Body. Caller should close resp.Body when done reading from it.
func PostJSON ¶
PostJSON issues a POST to the specified URL with the given body as JSON. values JSON as the request body.
The Content-Type header is set to application/json.
When err is nil, resp always contains a non-nil resp.Body. Caller should close resp.Body when done reading from it.
func Put ¶
func Put(url string, contentType string, body io.Reader, opts ...RequestOption) (resp *http.Response, err error)
Put issues a PUT to the specified URL.
When err is nil, resp always contains a non-nil resp.Body. Caller should close resp.Body when done reading from it.
func PutJSON ¶
PutJSON issues a PUT to the specified URL with the given body as JSON. values JSON as the request body.
The Content-Type header is set to application/json.
When err is nil, resp always contains a non-nil resp.Body. Caller should close resp.Body when done reading from it.
func ReadAnyJSON ¶
ReadAnyJSON read json from resp.Body
func ReadString ¶
ReadString read string from resp.Body and auto convert encoding
func WithContentType ¶
func WithContentType(contentType string) func(*RequestOptions)
WithContentType If a content type is set, each HTTP request will use this content type to make the request.
func WithDialTimeout ¶
func WithDialTimeout(dialTimeout time.Duration) func(*ClientOptions)
WithDialTimeout If a dial timeout is set, each HTTP request will use this time as the maximum limit for establishing a connection.
func WithHeaders ¶
func WithHeaders(headers http.Header) func(*RequestOptions)
WithHeaders If headers are set, each HTTP request will use these headers to make the request.
func WithKeepAliveTimeout ¶
func WithKeepAliveTimeout(keepAliveTimeout time.Duration) func(*ClientOptions)
WithKeepAliveTimeout If a keep-alive timeout is set, each HTTP request will use this time as the maximum limit for keeping the connection alive.
func WithProxy ¶
func WithProxy(proxy string) func(*ClientOptions)
WithProxy If a proxy is set, each HTTP request will use this proxy to make the request.
func WithReferer ¶
func WithReferer(referer string) func(*RequestOptions)
WithReferer If a referer is set, each HTTP request will use this referer to make the request.
func WithTLSHandshakeTimeout ¶
func WithTLSHandshakeTimeout(tlsHandshakeTimeout time.Duration) func(*ClientOptions)
WithTLSHandshakeTimeout If a TLS handshake timeout is set, each HTTP request will use this time as the maximum limit for completing the TLS handshake.
func WithUserAgent ¶
func WithUserAgent(userAgent string) func(*ClientOptions)
WithUserAgent sed to replace the default UserAgent when making a request.
Types ¶
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
func NewClient ¶
func NewClient(ctx context.Context, opts ...ClientOption) *Client
NewClient new client
func (*Client) Delete ¶
Delete issues a DELETE to the specified URL.
When err is nil, resp always contains a non-nil resp.Body. Caller should close resp.Body when done reading from it.
func (*Client) Do ¶
Do sends an HTTP request and returns an HTTP response, following policy (such as redirects, cookies, auth) as configured on the client.
An error is returned if caused by client policy (such as CheckRedirect), or failure to speak HTTP (such as a network connectivity problem). A non-2xx status code doesn't cause an error.
If the returned error is nil, the [Response] will contain a non-nil Body which the user is expected to close. If the Body is not both read to EOF and closed, the Client's underlying [RoundTripper] (typically [Transport]) may not be able to re-use a persistent TCP connection to the server for a subsequent "keep-alive" request.
The request Body, if non-nil, will be closed by the underlying Transport, even on errors. The Body may be closed asynchronously after Do returns.
On error, any Response can be ignored. A non-nil Response with a non-nil error only occurs when CheckRedirect fails, and even then the returned [Response.Body] is already closed.
Generally Get, Post, or PostForm will be used instead of Do.
If the server replies with a redirect, the Client first uses the CheckRedirect function to determine whether the redirect should be followed. If permitted, a 301, 302, or 303 redirect causes subsequent requests to use HTTP method GET (or HEAD if the original request was HEAD), with no body. A 307 or 308 redirect preserves the original HTTP method and body, provided that the [Request.GetBody] function is defined. The [NewRequest] function automatically sets GetBody for common standard library body types.
Any returned error will be of type *url.Error. The url.Error value's Timeout method will report true if the request timed out.
func (*Client) Get ¶
Get issues a GET to the specified URL.
When err is nil, resp always contains a non-nil resp.Body. Caller should close resp.Body when done reading from it.
func (*Client) Head ¶
Head issues a HEAD to the specified URL.
When err is nil, resp always contains a non-nil resp.Body. Caller should close resp.Body when done reading from it.
func (*Client) Patch ¶
func (c *Client) Patch(url string, contentType string, body io.Reader, opts ...RequestOption) (resp *http.Response, err error)
Patch issues a PATCH to the specified URL.
When err is nil, resp always contains a non-nil resp.Body. Caller should close resp.Body when done reading from it.
func (*Client) PatchJSON ¶
func (c *Client) PatchJSON(url string, body any, opts ...RequestOption) (resp *http.Response, err error)
PatchJSON issues a PATCH to the specified URL with the given body as JSON. values JSON as the request body.
The Content-Type header is set to application/json.
When err is nil, resp always contains a non-nil resp.Body. Caller should close resp.Body when done reading from it.
func (*Client) Post ¶
func (c *Client) Post(url string, contentType string, body io.Reader, opts ...RequestOption) (resp *http.Response, err error)
Post issues a POST to the specified URL.
When err is nil, resp always contains a non-nil resp.Body. Caller should close resp.Body when done reading from it.
func (*Client) PostForm ¶
func (c *Client) PostForm(url string, data url.Values, opts ...RequestOption) (resp *http.Response, err error)
PostForm issues a POST to the specified URL, with data's keys and values URL-encoded as the request body.
The Content-Type header is set to application/x-www-form-urlencoded.
When err is nil, resp always contains a non-nil resp.Body. Caller should close resp.Body when done reading from it.
func (*Client) PostFormFiles ¶
func (c *Client) PostFormFiles(url string, data url.Values, files []*UploadFile, opts ...RequestOption) (resp *http.Response, err error)
PostFormFiles issues a POST to the specified URL, with data's keys and values URL-encoded as the request body, and files as multipart form.
The Content-Type header is set to multipart/form-data.
When err is nil, resp always contains a non-nil resp.Body. Caller should close resp.Body when done reading from it.
func (*Client) PostJSON ¶
func (c *Client) PostJSON(url string, body any, opts ...RequestOption) (resp *http.Response, err error)
PostJSON issues a POST to the specified URL with the given body as JSON. values JSON as the request body.
The Content-Type header is set to application/json.
When err is nil, resp always contains a non-nil resp.Body. Caller should close resp.Body when done reading from it.
func (*Client) Put ¶
func (c *Client) Put(url string, contentType string, body io.Reader, opts ...RequestOption) (resp *http.Response, err error)
Put issues a PUT to the specified URL.
When err is nil, resp always contains a non-nil resp.Body. Caller should close resp.Body when done reading from it.
func (*Client) PutJSON ¶
func (c *Client) PutJSON(url string, body any, opts ...RequestOption) (resp *http.Response, err error)
PutJSON issues a PUT to the specified URL with the given body as JSON. values JSON as the request body.
The Content-Type header is set to application/json.
When err is nil, resp always contains a non-nil resp.Body. Caller should close resp.Body when done reading from it.
type ClientOptions ¶
type ClientOptions struct {
// contains filtered or unexported fields
}
ClientOptions http client options
type RequestOptions ¶
type RequestOptions struct {
// contains filtered or unexported fields
}
RequestOptions http request options