Skip to main content

Client

Struct Client 

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

An HTTP client.

use ntex::client::Client;

#[ntex::main]
async fn main() {
    let client = Client::new();

    let response = client
        .get("https://www.rust-lang.org")
        .header("User-Agent", "ntex")
        .send()
        .await;

    println!("Response: {response:?}");
}

§Shutdown

Clones of a client share its connection pools. A pool is stopped once the last clone and every request created from it are dropped, and dropping does not wait for connections to close:

  • Requests waiting for a connection fail with ConnectError::Disconnected.
  • Idle HTTP/1 connections are shut down in the background, bounded by the I/O shutdown timeout.
  • HTTP/2 connections stop accepting requests and are closed gracefully in the background once their in-flight requests have completed. Their closing is not bounded by a timeout.
  • Responses that are still being read keep their connections. An HTTP/1 connection is closed instead of being returned to the stopped pool.
  • A connection that is still being established is closed once the connect completes.

Implementations§

Source§

impl Client

Source

pub fn new() -> Client

Creates a client with default settings.

Source

pub fn builder() -> ClientBuilder

Creates a client builder.

Source

pub fn with_config(cfg: impl Into<SharedCfg>) -> Client

Creates a client with shared service configuration.

Source

pub async fn ready(&self) -> Result<(), Error<ClientError>>

Waits until the client is ready to process requests.

Source

pub fn request<U>(&self, method: Method, url: U) -> ClientRequest
where Url: TryFrom<U>, <Url as TryFrom<U>>::Error: Into<InvalidUrl>,

Creates an HTTP request with the specified method and URL.

Source

pub fn request_from<U>(&self, url: U, head: &RequestHead) -> ClientRequest
where Url: TryFrom<U>, <Url as TryFrom<U>>::Error: Into<InvalidUrl>,

Creates a ClientRequest from a RequestHead.

This is useful for proxy requests. The method and headers are copied from head; existing client default headers are not overwritten.

Source

pub fn get<U>(&self, url: U) -> ClientRequest
where Url: TryFrom<U>, <Url as TryFrom<U>>::Error: Into<InvalidUrl>,

Construct HTTP GET request.

Source

pub fn head<U>(&self, url: U) -> ClientRequest
where Url: TryFrom<U>, <Url as TryFrom<U>>::Error: Into<InvalidUrl>,

Construct HTTP HEAD request.

Source

pub fn put<U>(&self, url: U) -> ClientRequest
where Url: TryFrom<U>, <Url as TryFrom<U>>::Error: Into<InvalidUrl>,

Construct HTTP PUT request.

Source

pub fn post<U>(&self, url: U) -> ClientRequest
where Url: TryFrom<U>, <Url as TryFrom<U>>::Error: Into<InvalidUrl>,

Construct HTTP POST request.

Source

pub fn patch<U>(&self, url: U) -> ClientRequest
where Url: TryFrom<U>, <Url as TryFrom<U>>::Error: Into<InvalidUrl>,

Construct HTTP PATCH request.

Source

pub fn delete<U>(&self, url: U) -> ClientRequest
where Url: TryFrom<U>, <Url as TryFrom<U>>::Error: Into<InvalidUrl>,

Construct HTTP DELETE request.

Source

pub fn query<U>(&self, url: U) -> ClientRequest
where Url: TryFrom<U>, <Url as TryFrom<U>>::Error: Into<InvalidUrl>,

Construct HTTP QUERY request.

Source

pub fn options<U>(&self, url: U) -> ClientRequest
where Url: TryFrom<U>, <Url as TryFrom<U>>::Error: Into<InvalidUrl>,

Construct HTTP OPTIONS request.

Trait Implementations§

Source§

impl Clone for Client

Source§

fn clone(&self) -> Client

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 Debug for Client

Source§

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

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

impl Default for Client

Source§

fn default() -> Self

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

Auto Trait Implementations§

§

impl !Freeze for Client

§

impl !RefUnwindSafe for Client

§

impl !Send for Client

§

impl !Sync for Client

§

impl !UnwindSafe for Client

§

impl Unpin for Client

§

impl UnsafeUnpin for Client

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.