๐Ÿ“ฆ go-chi / transport

Go HTTP Client Middleware

โ˜… 11 stars โ‘‚ 2 forks ๐Ÿ‘ 11 watching โš–๏ธ MIT License
๐Ÿ“ฅ Clone https://github.com/go-chi/transport.git
HTTPS git clone https://github.com/go-chi/transport.git
SSH git clone git@github.com:go-chi/transport.git
CLI gh repo clone go-chi/transport
Vojtech Vitek (golang.cz) Vojtech Vitek (golang.cz) LogRequests: Log error on failed request (#16) dca46bf 23 hours ago ๐Ÿ“ History
๐Ÿ“‚ master View all commits โ†’
๐Ÿ“ .github
๐Ÿ“„ clone.go
๐Ÿ“„ curl.go
๐Ÿ“„ delayed_test.go
๐Ÿ“„ delayed.go
๐Ÿ“„ go.mod
๐Ÿ“„ go.sum
๐Ÿ“„ if_test.go
๐Ÿ“„ if.go
๐Ÿ“„ LICENSE
๐Ÿ“„ logRequests.go
๐Ÿ“„ README.md
๐Ÿ“„ retry.go
๐Ÿ“„ setHeader_test.go
๐Ÿ“„ setHeader.go
๐Ÿ“„ setHeaderFunc.go
๐Ÿ“„ transport_test.go
๐Ÿ“„ transport.go
๐Ÿ“„ README.md

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