Struct actix_test::ClientRequest

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§

§

impl ClientRequest

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

Set HTTP URI of request.

pub fn get_uri(&self) -> &Uri

Get HTTP URI of request.

pub fn address(self, addr: SocketAddr) -> ClientRequest

Set socket address of the server.

This address is used for connection. If address is not provided url’s host name get resolved.

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

Set HTTP method of this request.

pub fn get_method(&self) -> &Method

Get HTTP method of this request

pub fn get_version(&self) -> &Version

Get HTTP version of this request.

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

Get peer address of this request.

pub fn headers(&self) -> &HeaderMap

Returns request’s headers.

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

Returns request’s mutable headers.

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

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

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

Insert a header only if it is not yet set.

pub fn append_header(self, header: impl TryIntoHeaderPair) -> ClientRequest

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

pub fn camel_case(self) -> ClientRequest

Send headers in Camel-Case form.

pub fn force_close(self) -> ClientRequest

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

pub fn content_type<V>(self, value: V) -> ClientRequest

Set request’s content type

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

Set content length

pub fn basic_auth( self, username: impl Display, password: impl Display ) -> ClientRequest

Set HTTP basic authorization header.

If no password is needed, just provide an empty string.

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

Set HTTP bearer authentication header

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

Available on crate feature 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);

pub fn no_decompress(self) -> ClientRequest

Disable automatic decompress of response’s body

pub fn timeout(self, timeout: Duration) -> ClientRequest

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.

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

Sets the query part of the request

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

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

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

Complete request construction and send body.

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

Set a JSON body and generate ClientRequest

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

Set a urlencoded body and generate ClientRequest

ClientRequestBuilder can not be used after this call.

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.

pub fn send(self) -> SendClientRequest

Set an empty body and generate ClientRequest.

Trait Implementations§

§

impl Debug for ClientRequest

§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

source§

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

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Instrument for T

§

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

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

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 T
where U: From<T>,

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 for T

§

type Output = T

Should always be Self
source§

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

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

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

Performs the conversion.
source§

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

§

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

The type returned in the event of a conversion error.
source§

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

Performs the conversion.
§

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

§

fn vzip(self) -> V

§

impl<T> WithSubscriber for T

§

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
§

fn with_current_subscriber(self) -> WithDispatch<Self>

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