httpx

June 23, 2026 · View on GitHub

sink-logo

httpx

httpx provides lightweight helpers for making HTTP requests with less boilerplate.

It is designed to help you replace repetitive HTTP client code with small functions for:

  • sending requests for common HTTP methods
  • accessing raw responses when needed
  • configuring request behavior
  • decoding response bodies into typed values

Overview

Use httpx when you want HTTP client code to be easier to read, reuse, and test.

It is especially useful when:

  • the same request pattern appears in multiple places
  • a helper makes the intent of the code clearer
  • you want a consistent pattern for common HTTP methods instead of custom one-off clients

When to use it

Use httpx when:

  • you are writing client code that makes repeated HTTP requests
  • you want to reduce request boilerplate
  • you want clearer request and response handling around typed values

Prefer a simpler local implementation when:

  • the request is one-off and unlikely to be reused
  • you need fine-grained control over the HTTP client behavior
  • a helper would obscure important details at the call site

API reference

Send requests

FunctionPurpose
GetSends a GET request and returns a processed response
PostSends a POST request and returns a processed response
PutSends a PUT request and returns a processed response
PatchSends a PATCH request and returns a processed response
DeleteSends a DELETE request and returns a processed response

Access raw responses

FunctionPurpose
GetRawResponseSends a GET request and returns the raw response
PostRawResponseSends a POST request and returns the raw response
PutRawResponseSends a PUT request and returns the raw response
PatchRawResponseSends a PATCH request and returns the raw response
DeleteRawResponseSends a DELETE request and returns the raw response
HeadSends a HEAD request and returns the raw response
OptionsSends an OPTIONS request and returns the raw response

Low-level access

FunctionPurpose
DoRawRequestSends an HTTP request with a given method and body; returns the raw response
DoRequestSends an HTTP request with a given method and body; returns a decoded response
DecodeResponseDecodes a raw *http.Response into a typed Response[T]

Configure

TypePurpose
ConfigOptionsConfigures request behavior and response handling
NewOptionsCreates a new ConfigOptions instance

Options

OptionPurpose
WithHTTPClientSets the HTTP client used to send requests
WithHeaderAdds a single header to the request
WithHeadersMerges a set of headers into the request
WithTimeoutSets the request timeout; must be positive
WithParamAdds a single query parameter to the request
WithParamsMerges a set of query parameters into the request
StrictDecodingEnables strict JSON decoding; unknown fields cause an error
WithParseURLFuncOverrides the function used to parse the request URL
WithBearerTokenSets Authorization: Bearer <token>; token must not be empty
WithBasicAuthSets Authorization using HTTP Basic auth (RFC 7617); username required
WithUserAgentSets the User-Agent header; value must not be empty
WithInsecureSkipVerifyDisables TLS certificate verification; for testing only, not production

Errors

httpx returns sentinel errors for common failure cases. Use errors.Is to check which kind of error occurred:

  • ErrResponse — the server responded with a non-2xx status code.
  • ErrDecoding — the response body could not be decoded into the target type.
  • ErrTransport — the request failed to send or the response failed to read.
  • ErrEncoding — request payload serialization failed before the request was sent.
  • ErrOptions — one or more request options were invalid.

Typed errors

Each sentinel has a corresponding typed error that carries additional context. Use errors.As to access the fields:

TypeSentinelFields
*ResponseErrorErrResponseMethod, URL, StatusCode, Status, Body
*DecodingErrorErrDecodingContentType, Err
*EncodingErrorErrEncodingPayloadType, Err
*OptionsErrorErrOptionsOption, Message, Err

Each typed error has a constructor, useful when implementing custom options or URL parsers:

ConstructorPurpose
NewOptionsErrorReturns a new *OptionsError
NewEncodingErrorReturns a new *EncodingError
NewDecodingErrorReturns a new *DecodingError
NewResponseErrorReturns a new *ResponseError
NewTransportErrorWraps an error as an error joined with ErrTransport

Example:

resp, err := httpx.Get[MyType](ctx, url)
if err != nil {
    var respErr *httpx.ResponseError
    if errors.As(err, &respErr) {
        log.Printf("server returned %d for %s %s", respErr.StatusCode, respErr.Method, respErr.URL)
    }
}

Notes

  • Prefer the function that most clearly expresses your intent.
  • Use raw response helpers when you need direct access to the underlying response.
  • Use decoding helpers when you want to map response bodies into Go values.
  • Keep request options focused so the call site stays readable.

Examples