Setting Timeouts and Headers
Client timeouts, custom headers and auth tokens
Client-level timeout
Set http.Client.Timeout to bound the total time for request+response. This is the easiest timeout to configure.
client := &http.Client{Timeout: 10 * time.Second}Transport-level timeouts
Fine-tune connection establishment and TLS handshake timeouts via http.Transport:
t := &http.Transport{
DialContext: (&net.Dialer{
Timeout: 5 * time.Second,
KeepAlive: 30 * time.Second,
}).DialContext,
TLSHandshakeTimeout: 5 * time.Second,
ResponseHeaderTimeout: 5 * time.Second,
}
client := &http.Client{Transport: t, Timeout: 30 * time.Second}All lessons in this course
- Making HTTP Requests
- Setting Timeouts and Headers
- Decoding JSON Responses
- Error Handling and Retry Logic