Skip to main content

Client

Struct Client 

Source
pub struct Client { /* private fields */ }
Available on crate feature std only.
Expand description

A blocking HTTP/1.1 client: one connection per request.

SettingDefault
limitsLimits::new with a 16 MiB body
connect, read, and write timeout30 s each
total deadline for one exchange (connect, write, read)none

§Examples

Not run: it needs a server on port 8080.

use std::time::Duration;
use http_net::{blocking::Client, Limits};

let client = Client::new()
    .with_limits(Limits::new().with_max_body(64 << 20))
    .with_timeout(Duration::from_secs(5));
let resp = client.get("http://127.0.0.1:8080/big")?;

Implementations§

Source§

impl Client

Source

pub const fn new() -> Self

The defaults (see the table on Client).

§Examples
let _ = http_net::blocking::Client::new();
Source

pub const fn with_limits(self, limits: Limits) -> Self

Sets the response limits.

§Examples
let _ = http_net::blocking::Client::new().with_limits(http_net::Limits::new());
Source

pub const fn with_timeout(self, timeout: Duration) -> Self

Sets the connect, read, and write timeout (zero is raised to 1 ms).

§Examples
let _ = http_net::blocking::Client::new().with_timeout(std::time::Duration::from_secs(2));
Source

pub const fn with_deadline(self, deadline: Option<Duration>) -> Self

Sets a total deadline for one exchange: connecting, writing the request, and reading the whole response. The per-operation timeout of with_timeout cannot stop a server that sends one byte just inside it; this deadline can. None (the default) disables it.

§Examples
use std::time::Duration;
let _ = http_net::blocking::Client::new().with_deadline(Some(Duration::from_secs(10)));
Source

pub fn get(&self, url: &str) -> Result<ClientResponse, Error>

Fetches url with GET.

§Errors

As get.

§Examples

See Client.

Source

pub fn request( &self, url: &str, request: Request, ) -> Result<ClientResponse, Error>

Sends request to the http:// URL url and reads the response. The URL gives the authority (written as host) and the target, which replaces the target request was built with; the method, fields, and body come from request. The request is sent with connection: close on a connection of its own.

§Errors

As get; Error::Http with the builder’s first error; HttpError::InvalidHost when request already has a host (the URL supplies it); HttpError::InvalidState for a streamed body (chunked or content_length: this client sends full bodies) or an upgrade request (it cannot hand the connection over; use ClientConn). A Request::expect_continue request with its full body fails with Error::Http of HttpError::InvalidState (the expectation needs a streamed body).

§Examples
use http_net::{blocking::{self, Client}, Request, Response};

let server = blocking::Server::bind("127.0.0.1:0")?;
let addr = server.local_addr();
let stop = server.shutdown_handle();
let t = std::thread::spawn(move || {
    server.serve(|req, body| Response::text(200, &format!("{} {} {}", req.method_str(), req.path(), body.len())))
});

let put = Request::put("/").header("content-type", "application/json").body(&b"{}"[..]);
let resp = Client::new().request(&format!("http://{addr}/items/7"), put)?;
assert_eq!(resp.text(), Some("PUT /items/7 2"));

stop.shutdown();
t.join().unwrap()?;

Trait Implementations§

Source§

impl Clone for Client

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

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
Source§

impl Eq for Client

Source§

impl PartialEq for Client

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for Client

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> 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 = !

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.