Documentation
¶
Overview ¶
Package client provides a convenient HTTP client with support for common operations like GET, POST, PUT, PATCH, DELETE, file uploads, compression, and progress tracking.
Quick Start ¶
The simplest way to make requests is using the package-level functions:
resp, err := client.Get("https://httpbin.org/get")
if err != nil {
log.Fatal(err)
}
fmt.Println(resp.String())
Configuring Requests ¶
Use options.Option to customize requests with headers, compression, redirects, and progress tracking:
opt := options.New().
AddHeader("Authorization", "Bearer token").
SetCompression(options.CompressionGzip)
resp, err := client.Post(url, payload, opt)
Reusable Client ¶
For connection pooling and shared configuration, use Client:
c := client.New(options.New().
AddHeader("X-API-Key", "secret"))
resp1, _ := c.Get(url1)
resp2, _ := c.Get(url2) // reuses connections
File Uploads ¶
Upload files using PostFile, PutFile, or PatchFile:
resp, err := client.PostFile(url, "/path/to/file.txt")
Or pass an *os.File directly as payload:
file, _ := os.Open("data.json")
defer file.Close()
resp, err := client.Post(url, file, opt)
Compression ¶
Compress request payloads with gzip, deflate, or brotli:
opt := options.New().SetCompression(options.CompressionGzip) resp, err := client.Post(url, largePayload, opt)
Response decompression is automatic based on Content-Encoding headers.
Progress Tracking ¶
Monitor upload and download progress:
opt := options.New()
opt.Progress.OnUpload = func(bytesRead, totalBytes int64) {
fmt.Printf("Upload: %.1f%%\n", float64(bytesRead)/float64(totalBytes)*100)
}
opt.Progress.OnDownload = func(bytesRead, totalBytes int64) {
fmt.Printf("Download: %.1f%%\n", float64(bytesRead)/float64(totalBytes)*100)
}
Redirects ¶
Configure redirect behavior:
opt := options.New() opt.Redirect.Follow = true // follow redirects (default) opt.Redirect.PreserveMethod = true // keep POST on redirect opt.Redirect.Max = 10 // maximum redirects
Index ¶
- Constants
- Variables
- func Connect(url string, opts ...*options.Option) (response.Response, error)
- func Custom(method string, url string, payload any, opts ...*options.Option) (response.Response, error)
- func Delete(url string, opts ...*options.Option) (response.Response, error)
- func Get(url string, opts ...*options.Option) (response.Response, error)
- func Head(url string, opts ...*options.Option) (response.Response, error)
- func MultipartUpload(method, url string, payload map[string]any, opts ...*options.Option) (response.Response, error)
- func Options(url string, opts ...*options.Option) (response.Response, error)
- func Patch(url string, payload any, opts ...*options.Option) (response.Response, error)
- func PatchFile(url string, filename string, opts ...*options.Option) (response.Response, error)
- func PatchFormData(url string, payload map[string]string, opts ...*options.Option) (response.Response, error)
- func PatchMultipartUpload(url string, payload map[string]interface{}, opts ...*options.Option) (response.Response, error)
- func Post(url string, payload any, opts ...*options.Option) (response.Response, error)
- func PostFile(url string, filename string, opts ...*options.Option) (response.Response, error)
- func PostFormData(url string, payload map[string]string, opts ...*options.Option) (response.Response, error)
- func PostMultipartUpload(url string, payload map[string]interface{}, opts ...*options.Option) (response.Response, error)
- func Put(url string, payload any, opts ...*options.Option) (response.Response, error)
- func PutFile(url string, filename string, opts ...*options.Option) (response.Response, error)
- func PutFormData(url string, payload map[string]string, opts ...*options.Option) (response.Response, error)
- func PutMultipartUpload(url string, payload map[string]interface{}, opts ...*options.Option) (response.Response, error)
- func Trace(url string, opts ...*options.Option) (response.Response, error)
- type Client
- func (c *Client) AddGlobalOptions(opts *options.Option)
- func (c *Client) Clear()
- func (c *Client) CloneOptions() *options.Option
- func (c *Client) Connect(url string, opts ...*options.Option) (response.Response, error)
- func (c *Client) Custom(method string, url string, payload any, opts ...*options.Option) (response.Response, error)
- func (c *Client) Delete(url string, opts ...*options.Option) (response.Response, error)
- func (c *Client) Get(url string, opts ...*options.Option) (response.Response, error)
- func (c *Client) GlobalOptions() *options.Option
- func (c *Client) Head(url string, opts ...*options.Option) (response.Response, error)
- func (c *Client) Options(url string, opts ...*options.Option) (response.Response, error)
- func (c *Client) Patch(url string, payload any, opts ...*options.Option) (response.Response, error)
- func (c *Client) PatchFile(url string, filename string, opts ...*options.Option) (response.Response, error)
- func (c *Client) PatchFormData(url string, payload map[string]string, opts ...*options.Option) (response.Response, error)
- func (c *Client) Post(url string, payload any, opts ...*options.Option) (response.Response, error)
- func (c *Client) PostFile(url string, filename string, opts ...*options.Option) (response.Response, error)
- func (c *Client) PostFormData(url string, payload map[string]string, opts ...*options.Option) (response.Response, error)
- func (c *Client) Put(url string, payload any, opts ...*options.Option) (response.Response, error)
- func (c *Client) PutFile(url string, filename string, opts ...*options.Option) (response.Response, error)
- func (c *Client) PutFormData(url string, payload map[string]string, opts ...*options.Option) (response.Response, error)
- func (c *Client) Response(id string) *response.Response
- func (c *Client) ResponseCount() int
- func (c *Client) Responses() []response.Response
- func (c *Client) SetMaxResponses(max int)
- func (c *Client) SetResponseTTL(ttl time.Duration)
- func (c *Client) Trace(url string, opts ...*options.Option) (response.Response, error)
- func (c *Client) UpdateGlobalOptions(opts *options.Option)
Constants ¶
const ( DefaultMaxResponses = 1000 // Maximum number of responses to keep. DefaultResponseTTL = 5 * time.Minute // How long to keep responses before expiry. )
Default configuration for response history.
Variables ¶
var ( // ErrMaxRedirectsExceeded is returned when the number of redirects exceeds the configured maximum. ErrMaxRedirectsExceeded = errors.New("max redirects exceeded") // ErrEmptyURL is returned when an empty URL is provided. ErrEmptyURL = errors.New("empty URL") // ErrInvalidURL is returned when the URL cannot be parsed. ErrInvalidURL = errors.New("invalid URL") // ErrMissingHost is returned when the URL has no host component. ErrMissingHost = errors.New("missing host") // ErrRedirectMissingLocation is returned when a redirect response has no Location header. ErrRedirectMissingLocation = errors.New("redirect location header missing") // ErrPayloadNotReplayable is returned when a redirect or retry is needed but the // payload is a non-seekable io.Reader that exceeds the buffer limit and cannot be replayed. ErrPayloadNotReplayable = errors.New("payload cannot be replayed: non-seekable reader exceeds buffer limit") )
Sentinel errors for request handling
Functions ¶
func Connect ¶
Connect performs an HTTP CONNECT to the specified URL. It accepts the URL string as its first argument. Optionally, you can provide additional Options to customize the request. Returns the HTTP response and an error if any.
func Custom ¶
func Custom(method string, url string, payload any, opts ...*options.Option) (response.Response, error)
Custom performs a custom HTTP method to the specified URL with the given payload. It accepts the HTTP method as its first argument, the URL string as the second argument, the payload as the third argument, and optionally additional Options to customize the request. Returns the HTTP response and an error if any.
func Delete ¶
Delete performs an HTTP DELETE to the specified URL. It accepts the URL string as its first argument. Optionally, you can provide additional Options to customize the request. Returns the HTTP response and an error if any.
func Get ¶
Get performs an HTTP GET to the specified URL. It accepts the URL string as its first argument. Optionally, you can provide additional Options to customize the request. Returns the HTTP response and an error if any.
func Head ¶
Head performs an HTTP HEAD to the specified URL. It accepts the URL string as its first argument. Optionally, you can provide additional Options to customize the request. Returns the HTTP response and an error if any.
func MultipartUpload ¶
func MultipartUpload(method, url string, payload map[string]any, opts ...*options.Option) (response.Response, error)
MultipartUpload performs a multipart form-data upload request to the specified URL. It supports file uploads and other form fields.
func Options ¶
Options performs an HTTP OPTIONS to the specified URL. It accepts the URL string as its first argument. Optionally, you can provide additional Options to customize the request. Returns the HTTP response and an error if any.
func Patch ¶
Patch performs an HTTP PATCH to the specified URL with the given payload. It accepts the URL string as its first argument and the payload as the second argument. Optionally, you can provide additional Options to customize the request. Returns the HTTP response and an error if any.
func PatchFile ¶
PatchFile uploads a file to the specified URL using an HTTP PATCH request. It accepts the URL string as its first argument and the filename as the second argument. The file is read from the specified filename and uploaded as the request payload. Optionally, you can provide additional Options to customize the request. Returns the HTTP response and an error if any.
func PatchFormData ¶
func PatchFormData(url string, payload map[string]string, opts ...*options.Option) (response.Response, error)
PatchFormData performs an HTTP PATCH as an x-www-form-urlencoded payload to the specified URL. It accepts the URL string as its first argument and a map[string]string the payload. The map is converted to a url.QueryEscaped k/v pair that is sent to the server. Optionally, you can provide additional Options to customize the request. Returns the HTTP response and an error if any.
func PatchMultipartUpload ¶
func PatchMultipartUpload(url string, payload map[string]interface{}, opts ...*options.Option) (response.Response, error)
PatchMultipartUpload performs a PATCH multipart form-data upload request to the specified URL. This method can be used for partial updates to a resource, which might include updating or adding new file attachments.
func Post ¶
Post performs an HTTP POST to the specified URL with the given payload. It accepts the URL string as its first argument and the payload as the second argument. Optionally, you can provide additional Options to customize the request. Returns the HTTP response and an error if any.
func PostFile ¶
PostFile uploads a file to the specified URL using an HTTP POST request. It accepts the URL string as its first argument and the filename as the second argument. The file is read from the specified filename and uploaded as the request payload. Optionally, you can provide additional Options to customize the request. Returns the HTTP response and an error if any.
func PostFormData ¶
func PostFormData(url string, payload map[string]string, opts ...*options.Option) (response.Response, error)
PostFormData performs an HTTP POST as an x-www-form-urlencoded payload to the specified URL. It accepts the URL string as its first argument and a map[string]string the payload. The map is converted to a url.QueryEscaped k/v pair that is sent to the server. Optionally, you can provide additional Options to customize the request. Returns the HTTP response and an error if any.
func PostMultipartUpload ¶
func PostMultipartUpload(url string, payload map[string]interface{}, opts ...*options.Option) (response.Response, error)
PostMultipartUpload performs a POST multipart form-data upload request to the specified URL. This is the most common method for file uploads and creating new resources with file attachments.
func Put ¶
Put performs an HTTP PUT to the specified URL with the given payload. It accepts the URL string as its first argument and the payload as the second argument. Optionally, you can provide additional Options to customize the request. Returns the HTTP response and an error if any.
func PutFile ¶
PutFile uploads a file to the specified URL using an HTTP PUT request. It accepts the URL string as its first argument and the filename as the second argument. The file is read from the specified filename and uploaded as the request payload. Optionally, you can provide additional Options to customize the request. Returns the HTTP response and an error if any.
func PutFormData ¶
func PutFormData(url string, payload map[string]string, opts ...*options.Option) (response.Response, error)
PutFormData performs an HTTP PUT as an x-www-form-urlencoded payload to the specified URL. It accepts the URL string as its first argument and a map[string]string the payload. The map is converted to a url.QueryEscaped k/v pair that is sent to the server. Optionally, you can provide additional Options to customize the request. Returns the HTTP response and an error if any.
func PutMultipartUpload ¶
func PutMultipartUpload(url string, payload map[string]interface{}, opts ...*options.Option) (response.Response, error)
PutMultipartUpload performs a PUT multipart form-data upload request to the specified URL. This method is less common but can be used when updating an entire resource with new data, including file attachments.
Types ¶
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client represents a reusable HTTP client with persistent connection pooling. All requests made through a Client instance share the same underlying http.Client, enabling connection reuse and improved performance for multiple requests to the same hosts.
func New ¶
New returns a reusable Client with a persistent http.Client for connection pooling. Global options can be provided which will be applied to all subsequent requests.
func NewCustom ¶
NewCustom returns a reusable Client with a custom http.Client for connection pooling. Use this when you need specific http.Client configurations such as custom timeouts, transport settings, or TLS configuration. The provided client will be shared across all requests made through this Client instance.
func (*Client) AddGlobalOptions ¶
AddGlobalOptions merges the provided options into the client's existing global options. This preserves existing settings while adding or overwriting specific values from opts. Use UpdateGlobalOptions instead if you want to completely replace the global options.
func (*Client) Clear ¶
func (c *Client) Clear()
Clear clears any Responses that have already been made and kept.
func (*Client) CloneOptions ¶
CloneOptions returns a deep copy of the client's global options.
The returned Option can be modified without affecting the client's global configuration. This is used internally for per-request option merging and can be used externally to create request-specific variations.
func (*Client) Connect ¶
Connect performs an HTTP CONNECT to the specified URL. It accepts the URL string as its first argument. Optionally, you can provide additional Options to customize the request. Returns the HTTP response and an error if any.
func (*Client) Custom ¶
func (c *Client) Custom(method string, url string, payload any, opts ...*options.Option) (response.Response, error)
Custom performs a custom HTTP method to the specified URL with the given payload. It accepts the HTTP method as its first argument, the URL string as the second argument, the payload as the third argument, and optionally additional Options to customize the request. Returns the HTTP response and an error if any.
func (*Client) Delete ¶
Delete performs an HTTP DELETE to the specified URL. It accepts the URL string as its first argument. Optionally, you can provide additional Options to customize the request. Returns the HTTP response and an error if any.
func (*Client) Get ¶
Get performs an HTTP GET to the specified URL. It accepts the URL string as its first argument. Optionally, you can provide additional Options to customize the request. Returns the HTTP response and an error if any.
func (*Client) GlobalOptions ¶
GlobalOptions returns the global RequestOptions of the client.
func (*Client) Head ¶
Head performs an HTTP HEAD to the specified URL. It accepts the URL string as its first argument. Optionally, you can provide additional Options to customize the request. Returns the HTTP response and an error if any.
func (*Client) Options ¶
Options performs an HTTP OPTIONS to the specified URL. It accepts the URL string as its first argument. Optionally, you can provide additional Options to customize the request. Returns the HTTP response and an error if any.
func (*Client) Patch ¶
Patch performs an HTTP PATCH to the specified URL with the given payload. It accepts the URL string as its first argument and the payload as the second argument. Optionally, you can provide additional Options to customize the request. Returns the HTTP response and an error if any.
func (*Client) PatchFile ¶
func (c *Client) PatchFile(url string, filename string, opts ...*options.Option) (response.Response, error)
PatchFile uploads a file to the specified URL using an HTTP PATCH request. It accepts the URL string as its first argument and the filename as the second argument. The file is read from the specified filename and uploaded as the request payload. Optionally, you can provide additional Options to customize the request. Returns the HTTP response and an error if any.
func (*Client) PatchFormData ¶
func (c *Client) PatchFormData(url string, payload map[string]string, opts ...*options.Option) (response.Response, error)
PatchFormData performs an HTTP PATCH as an x-www-form-urlencoded payload to the specified URL. It accepts the URL string as its first argument and a map[string]string the payload. The map is converted to a url.QueryEscaped k/v pair that is sent to the server. Optionally, you can provide additional Options to customize the request. Returns the HTTP response and an error if any.
func (*Client) Post ¶
Post performs an HTTP POST to the specified URL with the given payload. It accepts the URL string as its first argument and the payload as the second argument. Optionally, you can provide additional Options to customize the request. Returns the HTTP response and an error if any.
func (*Client) PostFile ¶
func (c *Client) PostFile(url string, filename string, opts ...*options.Option) (response.Response, error)
PostFile uploads a file to the specified URL using an HTTP POST request. It accepts the URL string as its first argument and the filename as the second argument. The file is read from the specified filename and uploaded as the request payload. Optionally, you can provide additional Options to customize the request. Returns the HTTP response and an error if any.
func (*Client) PostFormData ¶
func (c *Client) PostFormData(url string, payload map[string]string, opts ...*options.Option) (response.Response, error)
PostFormData performs an HTTP POST as an x-www-form-urlencoded payload to the specified URL. It accepts the URL string as its first argument and a map[string]string the payload. The map is converted to a url.QueryEscaped k/v pair that is sent to the server. Optionally, you can provide additional Options to customize the request. Returns the HTTP response and an error if any.
func (*Client) Put ¶
Put performs an HTTP PUT to the specified URL with the given payload. It accepts the URL string as its first argument and the payload as the second argument. Optionally, you can provide additional Options to customize the request. Returns the HTTP response and an error if any.
func (*Client) PutFile ¶
func (c *Client) PutFile(url string, filename string, opts ...*options.Option) (response.Response, error)
PutFile uploads a file to the specified URL using an HTTP PUT request. It accepts the URL string as its first argument and the filename as the second argument. The file is read from the specified filename and uploaded as the request payload. Optionally, you can provide additional Options to customize the request. Returns the HTTP response and an error if any.
func (*Client) PutFormData ¶
func (c *Client) PutFormData(url string, payload map[string]string, opts ...*options.Option) (response.Response, error)
PutFormData performs an HTTP PUT as an x-www-form-urlencoded payload to the specified URL. It accepts the URL string as its first argument and a map[string]string the payload. The map is converted to a url.QueryEscaped k/v pair that is sent to the server. Optionally, you can provide additional Options to customize the request. Returns the HTTP response and an error if any.
func (*Client) Response ¶
Response retrieves a specific response by its UniqueIdentifier. Returns nil if the response is not found or has expired.
func (*Client) ResponseCount ¶
ResponseCount returns the number of stored responses (including expired ones that haven't been cleaned up yet).
func (*Client) Responses ¶
Responses returns a slice of all non-expired responses made by this Client. Responses are returned in no guaranteed order.
func (*Client) SetMaxResponses ¶
SetMaxResponses sets the maximum number of responses to keep. When the limit is exceeded, expired entries are cleaned up first, then oldest entries are removed if still over the limit.
func (*Client) SetResponseTTL ¶
SetResponseTTL sets how long responses are kept before being eligible for cleanup. Responses older than the TTL are removed during cleanup operations.
func (*Client) Trace ¶
Trace performs an HTTP TRACE to the specified URL. It accepts the URL string as its first argument. Optionally, you can provide additional Options to customize the request. Returns the HTTP response and an error if any.
func (*Client) UpdateGlobalOptions ¶
UpdateGlobalOptions replaces the client's global options entirely with the provided options. This discards all existing global settings. Use AddGlobalOptions instead if you want to merge new settings while preserving existing ones.
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
Package options provides configuration types for the HTTP client.
|
Package options provides configuration types for the HTTP client. |
|
Package progress provides utilities for displaying progress bars in the terminal.
|
Package progress provides utilities for displaying progress bars in the terminal. |
|
Package response provides the Response type returned by HTTP client operations.
|
Package response provides the Response type returned by HTTP client operations. |