Struct awc::ClientRequest
source · pub struct ClientRequest { /* private fields */ }
Expand description
An HTTP Client request builder
This type can be used to construct an instance of ClientRequest
through a
builder-like pattern.
let response = awc::Client::new()
.get("http://www.rust-lang.org") // <- Create request builder
.insert_header(("User-Agent", "Actix-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 address(self, addr: SocketAddr) -> Self
pub fn address(self, addr: SocketAddr) -> Self
Set socket address of the server.
This address is used for connection. If address is not provided url’s host name get resolved.
sourcepub fn get_method(&self) -> &Method
pub fn get_method(&self) -> &Method
Get HTTP method of this request
sourcepub fn get_version(&self) -> &Version
pub fn get_version(&self) -> &Version
Get HTTP version of this request.
sourcepub fn get_peer_addr(&self) -> &Option<SocketAddr>
pub fn get_peer_addr(&self) -> &Option<SocketAddr>
Get peer address of this request.
sourcepub fn headers_mut(&mut self) -> &mut HeaderMap
pub fn headers_mut(&mut self) -> &mut HeaderMap
Returns request’s mutable headers.
sourcepub fn insert_header(self, header: impl TryIntoHeaderPair) -> Self
pub fn insert_header(self, header: impl TryIntoHeaderPair) -> Self
Insert a header, replacing any that were set with an equivalent field name.
sourcepub fn insert_header_if_none(self, header: impl TryIntoHeaderPair) -> Self
pub fn insert_header_if_none(self, header: impl TryIntoHeaderPair) -> Self
Insert a header only if it is not yet set.
sourcepub fn append_header(self, header: impl TryIntoHeaderPair) -> Self
pub fn append_header(self, header: impl TryIntoHeaderPair) -> Self
Append a header, keeping any that were set with an equivalent field name.
use awc::{http::header, Client};
Client::new()
.get("http://www.rust-lang.org")
.insert_header(("X-TEST", "value"))
.insert_header((header::CONTENT_TYPE, mime::APPLICATION_JSON));
sourcepub fn camel_case(self) -> Self
pub fn camel_case(self) -> Self
Send headers in Camel-Case
form.
sourcepub fn force_close(self) -> Self
pub fn force_close(self) -> Self
Force close connection instead of returning it back to connections pool. This setting affect only HTTP/1 connections.
sourcepub fn content_type<V>(self, value: V) -> Self
pub fn content_type<V>(self, value: V) -> Self
Set request’s content type
sourcepub fn content_length(self, len: u64) -> Self
pub fn content_length(self, len: u64) -> Self
Set content length
sourcepub fn basic_auth(self, username: impl Display, password: impl Display) -> Self
pub fn basic_auth(self, username: impl Display, password: impl Display) -> Self
Set HTTP basic authorization header.
If no password is needed, just provide an empty string.
sourcepub fn bearer_auth(self, token: impl Display) -> Self
pub fn bearer_auth(self, token: impl Display) -> Self
Set HTTP bearer authentication header
Available on crate feature cookies
only.
cookies
only.Set a cookie
use awc::{cookie::Cookie, Client};
let res = Client::new().get("https://httpbin.org/cookies")
.cookie(Cookie::new("name", "value"))
.send()
.await;
println!("Response: {:?}", res);
sourcepub fn no_decompress(self) -> Self
pub fn no_decompress(self) -> Self
Disable automatic decompress of response’s body
sourcepub fn timeout(self, timeout: Duration) -> Self
pub fn timeout(self, timeout: Duration) -> Self
Set request timeout. Overrides client wide timeout setting.
Request timeout is the total time before a response must be received. Default value is 5 seconds.
sourcepub fn query<T: Serialize>(self, query: &T) -> Result<Self, Error>
pub fn query<T: Serialize>(self, query: &T) -> Result<Self, Error>
Sets the query part of the request
sourcepub fn freeze(self) -> Result<FrozenClientRequest, FreezeRequestError>
pub fn freeze(self) -> Result<FrozenClientRequest, FreezeRequestError>
Freeze request builder and construct FrozenClientRequest
,
which could be used for sending same request multiple times.
sourcepub fn send_body<B>(self, body: B) -> SendClientRequest ⓘwhere
B: MessageBody + 'static,
pub fn send_body<B>(self, body: B) -> SendClientRequest ⓘwhere
B: MessageBody + 'static,
Complete request construction and send body.
sourcepub fn send_json<T: Serialize>(self, value: &T) -> SendClientRequest ⓘ
pub fn send_json<T: Serialize>(self, value: &T) -> SendClientRequest ⓘ
Set a JSON body and generate ClientRequest
sourcepub fn send_form<T: Serialize>(self, value: &T) -> SendClientRequest ⓘ
pub fn send_form<T: Serialize>(self, value: &T) -> SendClientRequest ⓘ
Set a urlencoded body and generate ClientRequest
ClientRequestBuilder
can not be used after this call.
sourcepub fn send_stream<S, E>(self, stream: S) -> SendClientRequest ⓘ
pub fn send_stream<S, E>(self, stream: S) -> SendClientRequest ⓘ
Set an streaming body and generate ClientRequest
.
sourcepub fn send(self) -> SendClientRequest ⓘ
pub fn send(self) -> SendClientRequest ⓘ
Set an empty body and generate ClientRequest
.