pub struct HttpClient { /* private fields */ }Expand description
Async HTTP/1.1 client with a bounded keep-alive connection pool.
Implementations§
Source§impl HttpClient
impl HttpClient
Sourcepub fn new() -> Self
pub fn new() -> Self
New client trusting the Mozilla root CA set, 8 idle conns/host, 30s timeout.
Sourcepub fn with_tls(tls: TlsConnector) -> Self
pub fn with_tls(tls: TlsConnector) -> Self
New client with a caller-supplied TLS connector (e.g. custom roots).
Sourcepub fn set_timeout(&mut self, dur: Duration)
pub fn set_timeout(&mut self, dur: Duration)
Set the per-request timeout (applied to the full write+read cycle).
Sourcepub fn set_max_response_bytes(&mut self, n: usize)
pub fn set_max_response_bytes(&mut self, n: usize)
Set the maximum bytes buffered while parsing one response (headers +
body). Responses exceeding this — including a peer trickling bytes or
advertising an oversized Content-Length — are rejected with InvalidData.
Sourcepub fn set_max_idle_per_host(&mut self, n: usize)
pub fn set_max_idle_per_host(&mut self, n: usize)
Set the maximum idle connections retained per origin.
Sourcepub async fn request(
&self,
method: &str,
url: &str,
headers: &[(&str, &str)],
body: Option<&[u8]>,
) -> Result<Response>
pub async fn request( &self, method: &str, url: &str, headers: &[(&str, &str)], body: Option<&[u8]>, ) -> Result<Response>
Perform method url with optional body.
Reuses a pooled connection when available. If the pooled attempt fails (typically a server-closed idle socket), the request is retried once on a fresh connection only for idempotent methods (RFC 9110 §9.2.2: GET, HEAD, OPTIONS, TRACE, PUT, DELETE). A non-idempotent method (POST, PATCH) is never re-sent: the failed pooled attempt may already have executed server-side, so its error is surfaced to the caller instead. When the idempotent retry also fails, the returned error carries the discarded pooled-attempt error as its message context.
§Errors
Propagates URL-parse, connection, timeout, and protocol errors.