Skip to main content

HttpClient

Struct HttpClient 

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

Async HTTP/1.1 client with a bounded keep-alive connection pool.

Implementations§

Source§

impl HttpClient

Source

pub fn new() -> Self

New client trusting the Mozilla root CA set, 8 idle conns/host, 30s timeout.

Source

pub fn with_tls(tls: TlsConnector) -> Self

New client with a caller-supplied TLS connector (e.g. custom roots).

Source

pub fn set_timeout(&mut self, dur: Duration)

Set the per-request timeout (applied to the full write+read cycle).

Source

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.

Source

pub fn set_max_idle_per_host(&mut self, n: usize)

Set the maximum idle connections retained per origin.

Source

pub async fn get(&self, url: &str, headers: &[(&str, &str)]) -> Result<Response>

GET url.

§Errors

Propagates connection, timeout, and protocol errors.

Source

pub async fn head( &self, url: &str, headers: &[(&str, &str)], ) -> Result<Response>

HEAD url.

§Errors

Propagates connection, timeout, and protocol errors.

Source

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.

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<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, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.