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
impl Client
Sourcepub fn builder() -> ClientBuilder
pub fn builder() -> ClientBuilder
Creates a client builder.
Sourcepub fn with_config(cfg: impl Into<SharedCfg>) -> Client
pub fn with_config(cfg: impl Into<SharedCfg>) -> Client
Creates a client with shared service configuration.
Sourcepub async fn ready(&self) -> Result<(), Error<ClientError>>
pub async fn ready(&self) -> Result<(), Error<ClientError>>
Waits until the client is ready to process requests.
Sourcepub fn request<U>(&self, method: Method, url: U) -> ClientRequest
pub fn request<U>(&self, method: Method, url: U) -> ClientRequest
Creates an HTTP request with the specified method and URL.
Sourcepub fn request_from<U>(&self, url: U, head: &RequestHead) -> ClientRequest
pub fn request_from<U>(&self, url: U, head: &RequestHead) -> ClientRequest
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.
Sourcepub fn get<U>(&self, url: U) -> ClientRequest
pub fn get<U>(&self, url: U) -> ClientRequest
Construct HTTP GET request.
Sourcepub fn head<U>(&self, url: U) -> ClientRequest
pub fn head<U>(&self, url: U) -> ClientRequest
Construct HTTP HEAD request.
Sourcepub fn put<U>(&self, url: U) -> ClientRequest
pub fn put<U>(&self, url: U) -> ClientRequest
Construct HTTP PUT request.
Sourcepub fn post<U>(&self, url: U) -> ClientRequest
pub fn post<U>(&self, url: U) -> ClientRequest
Construct HTTP POST request.
Sourcepub fn patch<U>(&self, url: U) -> ClientRequest
pub fn patch<U>(&self, url: U) -> ClientRequest
Construct HTTP PATCH request.
Sourcepub fn delete<U>(&self, url: U) -> ClientRequest
pub fn delete<U>(&self, url: U) -> ClientRequest
Construct HTTP DELETE request.
Sourcepub fn query<U>(&self, url: U) -> ClientRequest
pub fn query<U>(&self, url: U) -> ClientRequest
Construct HTTP QUERY request.
Sourcepub fn options<U>(&self, url: U) -> ClientRequest
pub fn options<U>(&self, url: U) -> ClientRequest
Construct HTTP OPTIONS request.