Skip to main content

HttpClient

Struct HttpClient 

Source
pub struct HttpClient<S, D> { /* private fields */ }
Expand description

HTTP client. S = socket (BlockingSocketFactory), D = DNS (DnsResolver).

Clone shares the connection pool (and cookie store when enabled) via Arc.

use barehttp::HttpClient;

let client = HttpClient::new();
let response = client.get("http://example.com").call()?;

Implementations§

Source§

impl HttpClient<OsBlockingSocket, OsDnsResolver>

Source

pub fn new() -> Self

OS socket + DNS, default config.

Source

pub fn with_config(config: Config) -> Self

OS socket + DNS, custom config.

Source§

impl<S, D> HttpClient<S, D>

Source

pub fn with_adapters(dns: D, config: Config) -> Self

Custom DNS + config. Socket type S comes from S::new() at connect time (HttpClient::<OsBlockingSocket, _>::with_adapters(dns, config)).

Source

pub fn method( &self, method: Method, url: impl AsRef<str>, ) -> ClientRequestBuilder<S, D>

Request with an arbitrary method.

Source

pub fn get(&self, url: impl AsRef<str>) -> ClientRequestBuilder<S, D>

GET (no body).

Source

pub fn post(&self, url: impl AsRef<str>) -> ClientRequestBuilder<S, D>

Source

pub fn put(&self, url: impl AsRef<str>) -> ClientRequestBuilder<S, D>

Source

pub fn delete(&self, url: impl AsRef<str>) -> ClientRequestBuilder<S, D>

DELETE (no request body).

Source

pub fn head(&self, url: impl AsRef<str>) -> ClientRequestBuilder<S, D>

HEAD (no body).

Source

pub fn patch(&self, url: impl AsRef<str>) -> ClientRequestBuilder<S, D>

Source

pub fn cookie_store(&self) -> &CookieStore

Shared cookie store (cookie-jar feature).

Borrow of the store (not Arc); clone the client (or call again) to share without depending on the internal shared-ownership shape.

Source

pub fn config(&self) -> &Config

Shared client config.

Source

pub fn request( &self, method: Method, url: impl AsRef<str>, custom_headers: &Headers, body: Option<impl AsRef<[u8]>>, ) -> Result<Response, Error>

Redirect loop; each hop connect / write / read.

Body is copied once into an owned buffer (needed for redirect replay). Pass None::<&[u8]> when there is no body.

§Errors

Error::InvalidRequest for Method::Connect (no tunnel API), Error::InvalidUrl, Error::Dns, Error::Socket, Error::Parse, redirect / TLS / size-limit variants, or Error::HttpStatus when configured.

Source

pub fn request_with_config( &self, config: &Config, method: Method, url: impl AsRef<str>, custom_headers: &Headers, body: Option<impl AsRef<[u8]>>, ) -> Result<Response, Error>

Self::request with a caller-supplied config.

Body is copied once into an owned buffer (needed for redirect replay). Pass None::<&[u8]> when there is no body.

§Errors

Same as Self::request.

Trait Implementations§

Source§

impl<S, D> Clone for HttpClient<S, D>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<S, D> Debug for HttpClient<S, D>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for HttpClient<OsBlockingSocket, OsDnsResolver>

Source§

fn default() -> Self

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

Auto Trait Implementations§

§

impl<S, D> !RefUnwindSafe for HttpClient<S, D>

§

impl<S, D> !UnwindSafe for HttpClient<S, D>

§

impl<S, D> Freeze for HttpClient<S, D>

§

impl<S, D> Send for HttpClient<S, D>
where D: Sync + Send, S: Send,

§

impl<S, D> Sync for HttpClient<S, D>
where D: Sync + Send, S: Send,

§

impl<S, D> Unpin for HttpClient<S, D>

§

impl<S, D> UnsafeUnpin for HttpClient<S, D>

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.