pub struct ClientRequest { /* private fields */ }Expand description
An HTTP client request builder.
Builder methods configure the request and the send* methods consume it,
send the request, and return a ClientResponse.
use ntex::client::Client;
#[ntex::main]
async fn main() {
let response = Client::new()
.get("http://www.rust-lang.org") // <- Create request builder
.header("User-Agent", "ntex::web")
.send() // <- Send http request
.await;
response.and_then(|response| { // <- server http response
println!("Response: {:?}", response);
Ok(())
});
}Implementations§
Source§impl ClientRequest
impl ClientRequest
Sourcepub fn uri<U>(self, uri: U) -> Self
pub fn uri<U>(self, uri: U) -> Self
Sets the request URI.
URI conversion errors are stored and returned by the next send* call.
Sourcepub fn address(self, addr: SocketAddr) -> Self
pub fn address(self, addr: SocketAddr) -> Self
Sets the server socket address.
This address is used instead of resolving the URI host name. Pooled connections are reused only by requests with the same URI authority and the same address, so requests to one host with different addresses, or without one, never share a connection.
Sourcepub fn get_method(&self) -> &Method
pub fn get_method(&self) -> &Method
Returns the request method.
Sourcepub fn version(self, version: Version) -> Self
pub fn version(self, version: Version) -> Self
Sets the request HTTP version.
The version does not select the transport protocol. On an HTTP/1
connection, HTTP/1.0 is sent as requested and any other version is
sent as HTTP/1.1. An HTTP/2 connection always uses HTTP/2.
An HTTP/1.0 request closes the connection after the response unless
keep-alive is requested, and a streaming body needs a Content-Length
header because chunked coding is not available.
Sourcepub fn get_version(&self) -> &Version
pub fn get_version(&self) -> &Version
Returns the request HTTP version.
Sourcepub fn headers_mut(&mut self) -> &mut HeaderMap
pub fn headers_mut(&mut self) -> &mut HeaderMap
Returns mutable access to the request headers.
Sourcepub fn header<K, V>(self, key: K, value: V) -> Selfwhere
HeaderName: TryFrom<K>,
HeaderValue: TryFrom<V>,
<HeaderName as TryFrom<K>>::Error: Into<HttpError>,
<HeaderValue as TryFrom<V>>::Error: Into<HttpError>,
pub fn header<K, V>(self, key: K, value: V) -> Selfwhere
HeaderName: TryFrom<K>,
HeaderValue: TryFrom<V>,
<HeaderName as TryFrom<K>>::Error: Into<HttpError>,
<HeaderValue as TryFrom<V>>::Error: Into<HttpError>,
Append a header.
The header is appended to any existing values with the same name. Use
set_header to replace existing values.
Header conversion errors are stored and returned by the next send*
call.
use ntex::{http, client::Client};
#[ntex::main]
async fn main() {
let req = Client::new()
.get("http://www.rust-lang.org")
.header("X-TEST", "value")
.header(http::header::CONTENT_TYPE, "application/json");
}Sourcepub fn set_header<K, V>(self, key: K, value: V) -> Selfwhere
HeaderName: TryFrom<K>,
HeaderValue: TryFrom<V>,
<HeaderName as TryFrom<K>>::Error: Into<HttpError>,
<HeaderValue as TryFrom<V>>::Error: Into<HttpError>,
pub fn set_header<K, V>(self, key: K, value: V) -> Selfwhere
HeaderName: TryFrom<K>,
HeaderValue: TryFrom<V>,
<HeaderName as TryFrom<K>>::Error: Into<HttpError>,
<HeaderValue as TryFrom<V>>::Error: Into<HttpError>,
Inserts a header, replacing existing values with the same name.
Header conversion errors are stored and returned by the next send*
call.
Sourcepub fn set_header_if_none<K, V>(self, key: K, value: V) -> Selfwhere
HeaderName: TryFrom<K>,
HeaderValue: TryFrom<V>,
<HeaderName as TryFrom<K>>::Error: Into<HttpError>,
<HeaderValue as TryFrom<V>>::Error: Into<HttpError>,
pub fn set_header_if_none<K, V>(self, key: K, value: V) -> Selfwhere
HeaderName: TryFrom<K>,
HeaderValue: TryFrom<V>,
<HeaderName as TryFrom<K>>::Error: Into<HttpError>,
<HeaderValue as TryFrom<V>>::Error: Into<HttpError>,
Inserts a header if the request does not already contain one with the same name.
Header conversion errors are stored and returned by the next send*
call.
Sourcepub fn set_connection_type(self, ctype: ConnectionType) -> Self
pub fn set_connection_type(self, ctype: ConnectionType) -> Self
Sets the request connection type.
See RequestHead::set_connection_type()
for how repeated calls are resolved. On HTTP/1 connections, the close,
keep-alive and upgrade options of a Connection header set with
header are applied the same way. HTTP/2 connections
ignore the connection type.
Sourcepub fn force_close(self) -> Self
pub fn force_close(self) -> Self
Prevents an HTTP/1 connection from returning to the connection pool.
This setting affects only HTTP/1 connections.
Sourcepub fn content_type<V>(self, value: V) -> Self
pub fn content_type<V>(self, value: V) -> Self
Sets the request content type.
Header conversion errors are stored and returned by the next send*
call.
Sourcepub fn content_length(self, len: u64) -> Self
pub fn content_length(self, len: u64) -> Self
Sets the request content length.
A streaming body (see send_stream) is sent with
this length instead of chunked coding on HTTP/1 connections. Sending
fails if the stream ends early or produces more data. A body with a known
size is sent with its actual size and this value is ignored.
Sourcepub fn basic_auth<U>(self, username: U, password: Option<&str>) -> Selfwhere
U: Display,
pub fn basic_auth<U>(self, username: U, password: Option<&str>) -> Selfwhere
U: Display,
Sets the HTTP basic authentication header.
Replaces any existing Authorization header, including a client default.
Sourcepub fn bearer_auth<T>(self, token: T) -> Selfwhere
T: Display,
pub fn bearer_auth<T>(self, token: T) -> Selfwhere
T: Display,
Sets the HTTP bearer authentication header.
Replaces any existing Authorization header, including a client default.
Set a cookie.
When the request is sent, all cookies and every raw Cookie header
value are merged into a single Cookie header.
use coo_kie as cookie;
use ntex::client::Client;
#[ntex::main]
async fn main() {
let resp = Client::new().get("https://www.rust-lang.org")
.cookie(
cookie::Cookie::build(("name", "value"))
.domain("www.rust-lang.org")
.path("/")
.secure(true)
.http_only(true)
)
.send()
.await;
println!("Response: {:?}", resp);
}Sourcepub fn no_decompress(self) -> Self
pub fn no_decompress(self) -> Self
Disables automatic decompression of the response body.
Sourcepub fn timeout<T: Into<Millis>>(self, timeout: T) -> Self
pub fn timeout<T: Into<Millis>>(self, timeout: T) -> Self
Sets the response-header timeout for this request.
This overrides the client-wide timeout. The timeout covers receiving the
response head after the request has been sent. It does not cover waiting
for a pooled connection, connecting, sending the request body, or
reading the response body; use
MessageBody::timeout or
JsonBody::timeout for the body.
A zero duration disables only this timeout for this request.
The client-wide default is 5 seconds.
Sourcepub fn if_true<F>(self, value: bool, f: F) -> Self
pub fn if_true<F>(self, value: bool, f: F) -> Self
Applies f to this builder when value is true.
Source§impl ClientRequest
impl ClientRequest
Sourcepub fn send_body<B>(
self,
body: B,
) -> impl Future<Output = Result<ClientResponse, Error<ClientError>>>
pub fn send_body<B>( self, body: B, ) -> impl Future<Output = Result<ClientResponse, Error<ClientError>>>
Sends the request with body.
Sourcepub fn send_json<T: Serialize>(
self,
value: &T,
) -> impl Future<Output = Result<ClientResponse, Error<ClientError>>>
pub fn send_json<T: Serialize>( self, value: &T, ) -> impl Future<Output = Result<ClientResponse, Error<ClientError>>>
Serializes value as JSON and sends the request.
Sourcepub fn send_form<T: Serialize>(
self,
value: &T,
) -> impl Future<Output = Result<ClientResponse, Error<ClientError>>>
pub fn send_form<T: Serialize>( self, value: &T, ) -> impl Future<Output = Result<ClientResponse, Error<ClientError>>>
Serializes value as a URL-encoded form and sends the request.
Sourcepub fn send_stream<T, E>(
self,
stream: T,
) -> impl Future<Output = Result<ClientResponse, Error<ClientError>>>
pub fn send_stream<T, E>( self, stream: T, ) -> impl Future<Output = Result<ClientResponse, Error<ClientError>>>
Sends the request with a streaming body.
Sourcepub fn send(
self,
) -> impl Future<Output = Result<ClientResponse, Error<ClientError>>>
pub fn send( self, ) -> impl Future<Output = Result<ClientResponse, Error<ClientError>>>
Sends the request with an empty body.