A generics-first HTTP client wrapper for Go, built on top of the amazing github.com/imroc/req/v3 library.
It keeps req's power and escape hatches, while making the 90% use case feel effortless.
What is httpx?
httpx is a modern, ergonomic HTTP client for Go.
It builds on top of the amazing req library, preserving its flexibility while dramatically reducing boilerplate for common use cases. You get clean defaults, strong typing, and an API designed to stay out of your way.
Think of it as the 90% solution - powerful enough for real-world systems, without sacrificing clarity or control.
Quick Start
type Request struct {
Uuid string `json:"uuid"`
}
// request, binds result to Request
c := httpx.New()
res, _ := c.Get[Request]("https://httpbin.org/uuid")
httpx.Dump(res)
// #main.Request {
// +Uuid => "6101eccc-8f59-444f-9ccc-9c39a85d5da5" #string
// }
Installation
go get github.com/goforj/httpx/v2
The generic method API requires Go 1.27 or newer. Projects on older Go releases can pin the v2.0 release line.
Design Principles
- Simple by default - minimal configuration for common cases
- Composable - everything is opt-in and chainable
- Predictable - no hidden behavior or magic
- Typed - first-class generics with zero reflection
- Escape hatches included - full access to
req when needed
When to Use httpx
Use httpx when you want:
- A clean, expressive HTTP client without boilerplate
- Strong typing without fighting the type system
- Easy composition of headers, params, and behavior
- A smooth path from simple requests to advanced control
If you’re comfortable with net/http or req, httpx will feel immediately familiar - just faster to write and easier to read.
Debugging and Tracing
HTTP_TRACE=1 enables request/response dumps for all requests.
httpx.EnableDump() enables dump for a single request.
httpx.DumpEachRequest() enables per-request dumps on a client.
Documentation
The full API reference below is generated directly from source and always reflects the current codebase.
All runnable examples live in ./examples and are generated from doc comments.
They are compiled and executed in CI to ensure the documentation stays accurate and up to date.
v2 Status
httpx v1 has been tagged and is now frozen. The current module path is github.com/goforj/httpx/v2, and the main branch includes intentional breaking changes to improve API clarity and ergonomics (for example, request helpers return (T, error)).
Development
Use make test for the test suite, make vet for static checks, and make generate to refresh generated documentation. Keep examples and documentation accurate with behavior changes.
API Index
Auth
Auth
Auth sets the Authorization header using a scheme and token.
// Apply to all requests
c := httpx.New(httpx.Auth("Token", "abc123"))
res, _ := httpx.Get[map[string]any](c, "https://httpbin.org/headers")
httpx.Dump(res) // dumps map[string]any
// #map[string]interface {} {
// headers => #map[string]interface {} {
// Authorization => "Token abc123" #string
// }
// }
// Apply to a single request
res, _ = httpx.Get[map[string]any](c, "https://httpbin.org/headers", httpx.Auth("Token", "abc123"))
httpx.Dump(res) // dumps map[string]any
// #map[string]interface {} {
// headers => #map[string]interface {} {
// Authorization => "Token abc123" #string
// }
// }
Basic
Basic sets HTTP basic authentication headers.
// Apply to all requests
c := httpx.New(httpx.Basic("user", "pass"))
res, _ := httpx.Get[map[string]any](c, "https://httpbin.org/headers")
httpx.Dump(res) // dumps map[string]any
// #map[string]interface {} {
// headers => #map[string]interface {} {
// Authorization => "Basic dXNlcjpwYXNz" #string
// }
// }
// Apply to a single request
res, _ = httpx.Get[map[string]any](c, "https://httpbin.org/headers", httpx.Basic("user", "pass"))
httpx.Dump(res) // dumps map[string]any
// #map[string]interface {} {
// headers => #map[string]interface {} {
// Authorization => "Basic dXNlcjpwYXNz" #string
// }
// }
Bearer
Bearer sets the Authorization header with a bearer token.
// Apply to all requests
c := httpx.New(httpx.Bearer("token"))
res, _ := httpx.Get[map[string]any](c, "https://httpbin.org/headers")
httpx.Dump(res) // dumps map[string]any
// #map[string]interface {} {
// headers => #map[string]interface {} {
// Authorization => "Bearer token" #string
// }
// }
// Apply to a single request
res, _ = httpx.Get[map[string]any](c, "https://httpbin.org/headers", httpx.Bearer("token"))
httpx.Dump(res) // dumps map[string]any
// #map[string]interface {} {
// headers => #map[string]interface {} {
// Authorization => "Bearer token" #string
// }
// }
Browser Profiles
AsChrome
AsChrome applies the Chrome browser profile (headers including User-Agent, TLS, and HTTP/2 behavior).
_ = httpx.New(httpx.AsChrome())
AsFirefox
AsFirefox applies the Firefox browser profile (headers including User-Agent, TLS, and HTTP/2 behavior).
c := httpx.New(httpx.AsFirefox())
res, _ := httpx.Get[map[string]any](c, "https://httpbin.org/headers")
httpx.Dump(res) // dumps map[string]any
// #map[string]interface {} {
// headers => #map[string]interface {} {
// User-Agent => "<user-agent>" #string
// }
// }
AsMobile
AsMobile applies a mobile Chrome-like profile (headers including User-Agent, TLS, and HTTP/2 behavior).
_ = httpx.New(httpx.AsMobile())
AsSafari
AsSafari applies the Safari browser profile (headers including User-Agent, TLS, and HTTP/2 behavior).
_ = httpx.New(httpx.AsSafari())
Client
Client.Raw
Raw returns the underlying req client for chaining raw requests.
Client.Req
Req returns the underlying req client for advanced usage.
Default
Default returns the shared default client.
New
New creates a client with opinionated defaults and optional overrides.
var buf bytes.Buffer
_ = httpx.New(httpx.
BaseURL("https://httpbin.org").
Timeout(5*time.Second).
Header("X-Trace", "1").
Header("Accept", "application/json").
Transport(http.RoundTripper(http.DefaultTransport)).
Middleware(func(_ *req.Client, r *req.Request) error {
r.SetHeader("X-Middleware", "1")
return nil
}).
ErrorMapper(func(resp *req.Response) error {
return fmt.Errorf("status %d", resp.StatusCode)
}).
DumpAll().
DumpEachRequest().
DumpEachRequestTo(&buf).
Retry(func(rc *req.Client) {
rc.SetCommonRetryCount(2)
}).
RetryCount(2).
RetryFixedInterval(200*time.Millisecond).
RetryBackoff(100*time.Millisecond, 2*time.Second).
RetryInterval(func(_ *req.Response, attempt int) time.Duration {
return time.Duration(attempt) * 100 * time.Millisecond
}).
RetryCondition(func(resp *req.Response, _ error) bool {
return resp != nil && resp.StatusCode == 503
}).
RetryHook(func(_ *req.Response, _ error) {}),
)
Client Options
BaseURL
BaseURL sets a base URL on the client.
c := httpx.New(httpx.BaseURL("https://httpbin.org"))
res, _ := httpx.Get[map[string]any](c, "/uuid")
httpx.Dump(res) // dumps map[string]any
// #map[string]interface {} {
// uuid => "<uuid>" #string
// }
CookieJar
CookieJar sets the cookie jar for the client.
jar, _ := cookiejar.New(nil)
u, _ := url.Parse("https://httpbin.org")
jar.SetCookies(u, []*http.Cookie{
{Name: "session", Value: "abc123"},
})
c := httpx.New(httpx.CookieJar(jar))
res, _ := httpx.Get[map[string]any](c, "https://httpbin.org/cookies")
httpx.Dump(res) // dumps map[string]any
// #map[string]interface {} {
// cookies => #map[string]interface {} {
// session => "abc123" #string
// }
// }
ErrorMapper
ErrorMapper sets a custom error mapper for non-2xx responses.
c := httpx.New(httpx.ErrorMapper(func(resp *req.Response) error {
return fmt.Errorf("status %d", resp.StatusCode)
}))
res, _ := httpx.Get[map[string]any](c, "https://httpbin.org/status/500")
httpx.Dump(res) // dumps map[string]any
// map[string]interface {}(nil)
Middleware
Middleware adds request middleware to the client.
c := httpx.New(httpx.Middleware(func(_ *req.Client, r *req.Request) error {
r.SetHeader("X-Trace", "1")
return nil
}))
res, _ := httpx.Get[map[string]any](c, "https://httpbin.org/headers")
httpx.Dump(res) // dumps map[string]any
// #map[string]interface {} {
// headers => #map[string]interface {} {
// X-Trace => "1" #string
// }
// }
Proxy
Proxy sets a proxy URL for the client.
c := httpx.New(httpx.Proxy("http://localhost:8080"))
res, _ := httpx.Get[map[string]any](c, "https://httpbin.org/get")
httpx.Dump(res) // dumps map[string]any
// map[string]interface {}(nil)
ProxyFunc
ProxyFunc sets a proxy function for the client.
c := httpx.New(httpx.ProxyFunc(http.ProxyFromEnvironment))
res, _ := httpx.Get[map[string]any](c, "https://httpbin.org/uuid")
httpx.Dump(res) // dumps map[string]any
// #map[string]interface {} {
// uuid => "<uuid>" #string
// }
Redirect
Redirect sets the redirect policy for the client.
c := httpx.New(httpx.Redirect(req.NoRedirectPolicy()))
res, _ := httpx.Get[map[string]any](c, "https://httpbin.org/redirect/1")
httpx.Dump(res) // dumps map[string]any
// map[string]interface {}(nil)
Transport
Transport wraps the underlying transport with a custom RoundTripper.