go-chi / transport

Go HTTP Transports and middlewares

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Go HTTP transports & middlewares for outgoing HTTP requests

Chaining transports is a pattern originally inspired by this article https://dev.to/stevenacoffman/tripperwares-http-client-middleware-chaining-roundtrippers-3o00. This pattern is similar to middleware pattern which is used to enrich a context of http request coming to your application. There are multiple use-cases where this pattern comes handy such as request logging, caching, authentication and even implementation of retry mechanisms.

Examples

Set up HTTP client, which sets User-Agent, Authorization and TraceID headers automatically :

authClient := http.Client{
    Transport: transport.Chain(
        http.DefaultTransport,
        transport.SetHeader("User-Agent", userAgent),
        transport.SetHeader("Authorization", authHeader),
        transport.TraceID,
    ),
    Timeout: 15 * time.Second,
}

Or debug all outgoing requests globally within your application:

if debugMode {
    http.DefaultTransport = transport.Chain(
        http.DefaultTransport,
        transport.LogRequests,
    )
}

Authors

License

MIT license

About

Go HTTP Transports and middlewares

License:MIT License


Languages

Language:Go 100.0%