Struct Client

pub struct Client<C, B> { /* private fields */ }

A Client to make outgoing HTTP requests.

Client is cheap to clone and cloning is the recommended way to share a Client. The underlying connection pool will be reused.

Implementations

impl Client<(), ()>

fn builder<E>(executor: E) -> Builder
where
    E: Executor<Pin<Box<dyn Future<Output = ()> + Send>>> + Send + Sync + Clone + 'static,

Create a builder to configure a new Client.

Example

# #[cfg(feature = "tokio")]
# fn run () {
use std::time::Duration;
use hyper_util::client::legacy::Client;
use hyper_util::rt::{TokioExecutor, TokioTimer};

let client = Client::builder(TokioExecutor::new())
    .pool_timer(TokioTimer::new())
    .pool_idle_timeout(Duration::from_secs(30))
    .http2_only(true)
    .build_http();
# let infer: Client<_, http_body_util::Full<bytes::Bytes>> = client;
# drop(infer);
# }
# fn main() {}

impl<C, B> Client<C, B> where C: Connect + Clone + Send + Sync + 'static, B: Body + Send + 'static + Unpin, B::Data: Send, B::Error: Into<Box<dyn StdError + Send + Sync>>,

fn get(&self, uri: Uri) -> ResponseFuture
where
    B: Default,

Send a GET request to the supplied Uri.

Note

This requires that the Body type have a Default implementation. It should return an "empty" version of itself, such that Body::is_end_stream is true.

Example

# #[cfg(feature = "tokio")]
# fn run () {
use hyper::Uri;
use hyper_util::client::legacy::Client;
use hyper_util::rt::TokioExecutor;
use bytes::Bytes;
use http_body_util::Full;

let client: Client<_, Full<Bytes>> = Client::builder(TokioExecutor::new()).build_http();

let future = client.get(Uri::from_static("http://httpbin.org/ip"));
# }
# fn main() {}
fn request(&self, req: Request<B>) -> ResponseFuture

Send a constructed Request using this Client.

Example

# #[cfg(feature = "tokio")]
# fn run () {
use hyper::{Method, Request};
use hyper_util::client::legacy::Client;
use http_body_util::Full;
use hyper_util::rt::TokioExecutor;
use bytes::Bytes;

let client: Client<_, Full<Bytes>> = Client::builder(TokioExecutor::new()).build_http();

let req: Request<Full<Bytes>> = Request::builder()
    .method(Method::POST)
    .uri("http://httpbin.org/post")
    .body(Full::from("Hallo!"))
    .expect("request builder");

let future = client.request(req);
# }
# fn main() {}

Trait Implementations

impl<C, B> Debug for Client<C, B>

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

impl<C, B> Service<Request<B>> for Client<C, B> where C: Connect + Clone + Send + Sync + 'static, B: Body + Send + 'static + Unpin, B::Data: Send, B::Error: Into<Box<dyn StdError + Send + Sync>>,

type Response = Response<Incoming>;
type Error = Error;
type Future = ResponseFuture;
fn poll_ready(&mut self, &mut Context<'_>) -> Poll<Result<(), Self::Error>>
fn call(&mut self, req: Request<B>) -> Self::Future

impl<C: Clone, B> Clone for Client<C, B>

fn clone(&self) -> Client<C, B>

Auto Trait Implementations

impl<C, B> !RefUnwindSafe for Client<C, B>

impl<C, B> !UnwindSafe for Client<C, B>

impl<C, B> Freeze for Client<C, B> where C: Freeze, Pool<PoolClient<B>, (Scheme, Authority)>: Freeze,

impl<C, B> Send for Client<C, B> where C: Send, Pool<PoolClient<B>, (Scheme, Authority)>: Send,

impl<C, B> Sync for Client<C, B> where C: Sync, Pool<PoolClient<B>, (Scheme, Authority)>: Sync,

impl<C, B> Unpin for Client<C, B> where C: Unpin, Pool<PoolClient<B>, (Scheme, Authority)>: Unpin,

impl<C, B> UnsafeUnpin for Client<C, B> where C: UnsafeUnpin, Pool<PoolClient<B>, (Scheme, Authority)>: UnsafeUnpin,

Blanket Implementations

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

fn type_id(&self) -> TypeId

impl<T> Borrow<T> for Client<C, B> where T: ?Sized,

fn borrow(&self) -> &T

impl<T> BorrowMut<T> for Client<C, B> where T: ?Sized,

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

impl<T> CloneToUninit for Client<C, B> where T: Clone,

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

impl<T> From<T> for Client<C, B>

fn from(t: T) -> T

Returns the argument unchanged.

impl<T> Instrument for Client<C, B>

impl<T> ToOwned for Client<C, B> where T: Clone,

type Owned = T;
fn to_owned(&self) -> T
fn clone_into(&self, target: &mut T)

impl<T> WithSubscriber for Client<C, B>

impl<T, U> Into<U> for Client<C, B> where U: From<T>,

fn into(self) -> U

Calls U::from(self).

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

impl<T, U> TryFrom<U> for Client<C, B> where U: Into<T>,

type Error = never;
fn try_from(value: U) -> Result<T, never>

impl<T, U> TryInto<U> for Client<C, B> where U: TryFrom<T>,

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