pub struct HttpClient { /* private fields */ }Expand description
Async HTTP/1.1 client with bounded redirects and connection reuse.
Implementations§
Source§impl HttpClient
impl HttpClient
Sourcepub fn new() -> Self
pub fn new() -> Self
Create a client using Mozilla roots and bounded default policies.
Defaults are eight idle connections per origin, a five-minute idle
lifetime, ten followed redirects, a 30-second logical-request deadline,
and the response limit documented by DEFAULT_MAX_RESPONSE_BYTES.
Sourcepub fn with_tls(tls: TlsConnector) -> Self
pub fn with_tls(tls: TlsConnector) -> Self
Create a client with a caller-supplied TLS connector.
Sourcepub fn set_timeout(&mut self, duration: Duration)
pub fn set_timeout(&mut self, duration: Duration)
Set the deadline for the complete logical request.
The deadline covers connection acquisition, a stale pooled-connection retry, every redirect hop, request writes, and response reads.
Sourcepub fn set_max_response_bytes(&mut self, bytes: usize)
pub fn set_max_response_bytes(&mut self, bytes: usize)
Set the maximum bytes buffered while parsing one response.
The limit includes response headers and body. A peer exceeding it is
rejected with io::ErrorKind::InvalidData.
Sourcepub fn set_max_idle_per_host(&mut self, connections: usize)
pub fn set_max_idle_per_host(&mut self, connections: usize)
Set the maximum idle connections retained per origin.
Zero disables retention without creating empty origin buckets.
Sourcepub fn set_idle_timeout(&mut self, duration: Duration)
pub fn set_idle_timeout(&mut self, duration: Duration)
Set how long a pooled connection remains eligible for reuse.
Expiry is checked when the pool is accessed, avoiding a background task. Zero disables reuse of connections subsequently returned to the pool.
Sourcepub fn set_max_redirects(&mut self, redirects: usize)
pub fn set_max_redirects(&mut self, redirects: usize)
Set the maximum redirect hops followed by one logical request.
Zero rejects the first redirect that carries a Location field.
Sourcepub async fn get(&self, url: &str, headers: &[(&str, &str)]) -> Result<Response>
pub async fn get(&self, url: &str, headers: &[(&str, &str)]) -> Result<Response>
Perform a GET request.
§Errors
Returns URL, connection, timeout, redirect-policy, or protocol errors.
Sourcepub async fn head(
&self,
url: &str,
headers: &[(&str, &str)],
) -> Result<Response>
pub async fn head( &self, url: &str, headers: &[(&str, &str)], ) -> Result<Response>
Perform a HEAD request.
§Errors
Returns URL, connection, timeout, redirect-policy, or protocol errors.
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, following bounded HTTP redirects.
301 and 302 rewrite POST to a bodyless GET for deployed compatibility;
303 rewrites every method except HEAD to GET; 307 and 308 preserve
method and body. Destination-specific and hop-by-hop fields are removed,
and credentials are not forwarded across origins. A redirect response
without Location is returned to the caller unchanged.
A failed pooled exchange is retried once on a fresh connection only for idempotent methods. The single configured timeout encloses retries and redirects rather than restarting at each hop.
§Errors
Returns URL, connection, timeout, redirect-limit, redirect-location, or protocol errors.