pub struct Client { /* private fields */ }std only.Expand description
A blocking HTTP/1.1 client: one connection per request.
| Setting | Default |
|---|---|
| limits | Limits::new with a 16 MiB body |
| connect, read, and write timeout | 30 s each |
| total deadline for one exchange (connect, write, read) | none |
§Examples
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
impl Client
Sourcepub const fn with_limits(self, limits: Limits) -> Self
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());Sourcepub const fn with_timeout(self, timeout: Duration) -> Self
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));Sourcepub const fn with_deadline(self, deadline: Option<Duration>) -> Self
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)));Sourcepub fn request(
&self,
url: &str,
request: Request,
) -> Result<ClientResponse, Error>
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).
§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()?;