http.Client
The Go type that sends HTTP requests and returns responses, managing connections and timeouts for you.
In more detail
Create one and reuse it so connections are pooled. Set its Timeout or pass a context with a deadline on each request; http.DefaultClient has no timeout, which can hang a tool forever. Do returns an error only for transport failures; a 500 response is not an error, so check the status code yourself.
Example
client := &http.Client{Timeout: 5 * time.Second}Related terms
- HTTP request — The message a client sends to a server: a method, a target, a protocol version, headers, a blank line and an optional body.
- HTTP response — The message a server sends back: a protocol version, a status code and phrase, headers, a blank line and usually a body.
- http.RoundTripper — The Go interface for the part of an HTTP client that actually sends one request and returns one response.
- timeout — A limit on how long a caller waits for an answer before giving up.
- context deadline — A time limit attached to a context, after which it is cancelled automatically and ctx.Err() reports context.DeadlineExceeded.
Taught in
- Calling APIs with net/http Client — Concurrent Systems Programming with Go