Documentation
¶
Overview ¶
Package httpclient provides a thin HTTP client wrapper with helpers for common header presets, query parameter encoding, and response decompression.
Sending a request ¶
body, status, err := httpclient.NewRequestWithContext(
ctx,
http.MethodGet,
"https://api.example.com/users",
map[string]string{"page": "1"},
httpclient.DefaultJSONHeaders(),
nil,
)
Decompression ¶
DecompressResponse wraps the response body in the correct reader based on the Content-Encoding header. Use it when the server returns a compressed response and you need a plain io.ReadCloser.
resp, err := http.DefaultClient.Do(req) reader, err := httpclient.DecompressResponse(resp) defer reader.Close()
Supported encodings: gzip, deflate, br (Brotli), zstd, and identity.
Index ¶
Constants ¶
const ( HeaderContentType = "Content-Type" HeaderAccept = "Accept" HeaderAuthorization = "Authorization" HeaderUserAgent = "User-Agent" HeaderAcceptEncoding = "Accept-Encoding" HeaderContentEncoding = "Content-Encoding" HeaderCacheControl = "Cache-Control" HeaderXRequestID = "X-Request-Id" )
Header name constants.
const ( MIMEApplicationJSON = "application/json" MIMEApplicationXML = "application/xml" MIMEApplicationFormURLEncoded = "application/x-www-form-urlencoded" MIMEMultipartFormData = "multipart/form-data" MIMETextPlain = "text/plain; charset=utf-8" MIMEOctetStream = "application/octet-stream" )
MIME type constants.
const ( CacheControlNoCache = "no-cache" CacheControlNoStore = "no-store" CacheControlMaxAge0 = "max-age=0" )
Cache-Control directive constants.
const AcceptEncodingAll = "gzip, deflate, br, zstd"
AcceptEncodingAll declares support for all encodings implemented in DecompressResponse. Use alongside DecompressResponse.
Variables ¶
This section is empty.
Functions ¶
func DecompressResponse ¶
func DecompressResponse(r *http.Response) (io.ReadCloser, error)
DecompressResponse wraps the response body in the appropriate decompression reader based on the Content-Encoding header. The caller is responsible for closing the returned reader.
func NewRequestWithContext ¶
func NewRequestWithContext( ctx context.Context, method string, urlStr string, queryParams map[string]string, headers map[string]string, payload []byte, client ...*http.Client, ) ([]byte, int, error)
NewRequestWithContext builds and executes an HTTP request, returning the raw response body, the status code, and any error. Decompression is not applied automatically use DecompressResponse if needed.
Types ¶
type MapParams ¶
MapParams is a map type used by this package for request headers and query parameters.
func DefaultCompressedHeaders ¶
func DefaultCompressedHeaders() MapParams
DefaultCompressedHeaders returns a new map that advertises support for all compressed encodings. Use alongside DecompressResponse.
func DefaultFormHeaders ¶
func DefaultFormHeaders() MapParams
DefaultFormHeaders returns a new map with standard form-encoded request headers.
func DefaultJSONHeaders ¶
func DefaultJSONHeaders() MapParams
DefaultJSONHeaders returns a new map with standard JSON request headers.