Skip to content

Repository files navigation

HTTP Client interceptor

HTTP client interceptor system for Go. Compose middleware around http.RoundTripper to inspect, modify, retry, or short-circuit requests and responses, enabling a clean and extensible way to enrich and control outgoing HTTP calls.

Install

go get github.com/fervbmx/interceptor

Usage

Import both packages:

import (
"net/http""github.com/fervbmx/interceptor""github.com/fervbmx/interceptor/interceptors"
)
// Flow: RequestLogging → Header → BasicAuth → http.DefaultTransportclient:=&http.Client{
Transport: interceptor.NewTransport(
nil,
interceptors.Header("X-API-KEY", "secret"),
interceptors.BasicAuth("user", "pass"),
interceptors.RequestLogging(nil),
),
}

Pass nil as the first argument to use http.DefaultTransport, or pass a custom http.RoundTripper as the base transport.

Built-in interceptors

InterceptorDescription
Header(key, value)Sets a header on every request
BasicAuth(user, password)Sets Basic authentication
RequestLogging(opts)Emits structured slog attributes

Custom interceptors

Write your own interceptor.Middleware to hook into the request/response lifecycle. Call next to continue the chain, or return early to short-circuit it.

// Log every request and its status code.funcLogging(req*http.Request, next interceptor.HandlerFunc) (*http.Response, error) {
log.Printf("→ %s %s", req.Method, req.URL)
resp, err:=next(req)
iferr!=nil {
returnnil, err
}
log.Printf("%s %s → %d", req.Method, req.URL, resp.StatusCode)
returnresp, nil
}
client:=&http.Client{
Transport: interceptor.NewTransport(
http.DefaultTransport,
Logging,
),
}

License

MIT

About

HTTP client interceptor system for Go. Compose middleware around http.RoundTripper to inspect, modify, retry, or short-circuit requests and responses, enabling a clean and extensible way to enrich and control outgoing HTTP calls.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages