Skip to content

go-chi/transport

Repository files navigation

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:

import (
    "github.com/go-chi/traceid"
)

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

Or debug all outgoing requests as curl globally within your application:

debugMode := os.Getenv("DEBUG") == "true"

http.DefaultTransport = transport.Chain(
    http.DefaultTransport,
    transport.If(debugMode, transport.LogRequests(transport.LogOptions{Concise: true, CURL: true})),
)

Authors

License

MIT license

About

Go HTTP Transports and middlewares

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages