๐Ÿ“ฆ go-chi / transport

๐Ÿ“„ README.md ยท 43 lines
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43# 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:
```go
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:
```go
debugMode := os.Getenv("DEBUG") == "true"

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

# Authors
- [Golang.cz](https://golang.cz/)
- See [list of contributors](https://github.com/go-chi/transport/graphs/contributors).

# License
[MIT license](./LICENSE)