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.
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.
By default, the version is selected from the negotiated transport protocol.
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.
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.
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.
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. A zero duration disables the 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 async fn send_body<B>(
self,
body: B,
) -> Result<ClientResponse, Error<ClientError>>
pub async fn send_body<B>( self, body: B, ) -> Result<ClientResponse, Error<ClientError>>
Sends the request with body.
Sourcepub async fn send_json<T: Serialize>(
self,
value: &T,
) -> Result<ClientResponse, Error<ClientError>>
pub async fn send_json<T: Serialize>( self, value: &T, ) -> Result<ClientResponse, Error<ClientError>>
Serializes value as JSON and sends the request.
Sourcepub async fn send_form<T: Serialize>(
self,
value: &T,
) -> Result<ClientResponse, Error<ClientError>>
pub async fn send_form<T: Serialize>( self, value: &T, ) -> Result<ClientResponse, Error<ClientError>>
Serializes value as a URL-encoded form and sends the request.
Sourcepub async fn send_stream<T, E>(
self,
stream: T,
) -> Result<ClientResponse, Error<ClientError>>
pub async fn send_stream<T, E>( self, stream: T, ) -> Result<ClientResponse, Error<ClientError>>
Sends the request with a streaming body.
Sourcepub async fn send(self) -> Result<ClientResponse, Error<ClientError>>
pub async fn send(self) -> Result<ClientResponse, Error<ClientError>>
Sends the request with an empty body.