Expand description
Outbound HTTP client
Services call other services. HttpClient is a small client for that with
the settings production calls need and ad-hoc code forgets:
- timeouts for connecting and for the whole attempt, and a cap on the response size
- connection pooling (keep-alive) per host
- retries with exponential backoff and jitter for connection failures and
502/503/504/429(honouringRetry-After), only for requests that are safe to repeat: idempotent methods or requests carrying anIdempotency-Key - redirects (followed, up to a limit; credentials are dropped when the target is another origin)
- an SSRF guard (
HttpClient::public_only): every connection, including those after a redirect, goes to a vetted public address - HTTPS with the Mozilla root certificates (feature
client-tls)
use std::time::Duration;
use ullun::client::HttpClient;
let client = HttpClient::new()
.timeout(Duration::from_secs(5))
.retries(2)
.public_only();
let res = client.get("http://example.com/api/items?limit=10").send().await?;
if res.status() == 200 {
let items: serde_json::Value = res.json()?;
println!("{items}");
}ClientError converts into Error (504 for
timeouts, 502 otherwise), so handlers can use ? on client calls.
Structs§
- Client
Response - What the server answered
- Http
Client - HTTP client, see the module documentation. Cloning shares the connection pool.
- Request
Builder - A request under construction, created by the verbs of
HttpClient
Enums§
- Client
Error - Why a client call failed