Skip to main content

HttpClient

Struct HttpClient 

Source
pub struct HttpClient { /* private fields */ }
Expand description

Async HTTP/1.1 client with bounded redirects and connection reuse.

Implementations§

Source§

impl HttpClient

Source

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.

Source

pub fn with_tls(tls: TlsConnector) -> Self

Create a client with a caller-supplied TLS connector.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Trait Implementations§

Source§

impl Default for HttpClient

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V