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

source

pub fn uri<U>(self, uri: U) -> Selfwhere Uri: TryFrom<U>, <Uri as TryFrom<U>>::Error: Into<HttpError>,

Set HTTP URI of request.

source

pub fn get_uri(&self) -> &Uri

Get HTTP URI of request.

source

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.

source

pub fn method(self, method: Method) -> Self

Set HTTP method of this request.

source

pub fn get_method(&self) -> &Method

Get HTTP method of this request

source

pub fn get_version(&self) -> &Version

Get HTTP version of this request.

source

pub fn get_peer_addr(&self) -> &Option<SocketAddr>

Get peer address of this request.

source

pub fn headers(&self) -> &HeaderMap

Returns request’s headers.

source

pub fn headers_mut(&mut self) -> &mut HeaderMap

Returns request’s mutable headers.

source

pub fn insert_header(self, header: impl TryIntoHeaderPair) -> Self

Insert a header, replacing any that were set with an equivalent field name.

source

pub fn insert_header_if_none(self, header: impl TryIntoHeaderPair) -> Self

Insert a header only if it is not yet set.

source

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));
source

pub fn camel_case(self) -> Self

Send headers in Camel-Case form.

source

pub fn force_close(self) -> Self

Force close connection instead of returning it back to connections pool. This setting affect only HTTP/1 connections.

source

pub fn content_type<V>(self, value: V) -> Selfwhere HeaderValue: TryFrom<V>, <HeaderValue as TryFrom<V>>::Error: Into<HttpError>,

Set request’s content type

source

pub fn content_length(self, len: u64) -> Self

Set content length

source

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.

source

pub fn bearer_auth(self, token: impl Display) -> Self

Set HTTP bearer authentication header

source

pub fn cookie(self, cookie: Cookie<'_>) -> Self

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);
source

pub fn no_decompress(self) -> Self

Disable automatic decompress of response’s body

source

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.

source

pub fn query<T: Serialize>(self, query: &T) -> Result<Self, Error>

Sets the query part of the request

source

pub fn freeze(self) -> Result<FrozenClientRequest, FreezeRequestError>

Freeze request builder and construct FrozenClientRequest, which could be used for sending same request multiple times.

source

pub fn send_body<B>(self, body: B) -> SendClientRequest where B: MessageBody + 'static,

Complete request construction and send body.

source

pub fn send_json<T: Serialize>(self, value: &T) -> SendClientRequest

Set a JSON body and generate ClientRequest

source

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.

source

pub fn send_stream<S, E>(self, stream: S) -> SendClientRequest where S: Stream<Item = Result<Bytes, E>> + 'static, E: Into<Box<dyn Error>> + 'static,

Set an streaming body and generate ClientRequest.

source

pub fn send(self) -> SendClientRequest

Set an empty body and generate ClientRequest.

Trait Implementations§

source§

impl Debug for ClientRequest

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

const: unstable · source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T> Instrument for T

source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
source§

impl<T, U> Into<U> for Twhere U: From<T>,

const: unstable · source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> Same<T> for T

§

type Output = T

Should always be Self
source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
const: unstable · source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<V, T> VZip<V> for Twhere V: MultiLane<T>,

§

fn vzip(self) -> V

source§

impl<T> WithSubscriber for T

source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more