crux_http

Struct ResponseAsync

source
pub struct ResponseAsync { /* private fields */ }
Expand description

An HTTP response that exposes async methods. This is to support async use and middleware.

Implementations§

source§

impl ResponseAsync

source

pub fn status(&self) -> StatusCode

Get the HTTP status code.

§Examples
let res = client.get("https://httpbin.org/get").await?;
assert_eq!(res.status(), 200);
source

pub fn version(&self) -> Option<Version>

Get the HTTP protocol version.

§Examples
use crux_http::http::Version;

let res = client.get("https://httpbin.org/get").await?;
assert_eq!(res.version(), Some(Version::Http1_1));
source

pub fn header(&self, name: impl Into<HeaderName>) -> Option<&HeaderValues>

Get a header.

§Examples
let res = client.get("https://httpbin.org/get").await?;
assert!(res.header("Content-Length").is_some());
source

pub fn header_mut( &mut self, name: impl Into<HeaderName>, ) -> Option<&mut HeaderValues>

Get an HTTP header mutably.

source

pub fn remove_header( &mut self, name: impl Into<HeaderName>, ) -> Option<HeaderValues>

Remove a header.

source

pub fn insert_header( &mut self, key: impl Into<HeaderName>, value: impl ToHeaderValues, )

Insert an HTTP header.

source

pub fn append_header( &mut self, key: impl Into<HeaderName>, value: impl ToHeaderValues, )

Append an HTTP header.

source

pub fn iter(&self) -> Iter<'_>

An iterator visiting all header pairs in arbitrary order.

source

pub fn iter_mut(&mut self) -> IterMut<'_>

An iterator visiting all header pairs in arbitrary order, with mutable references to the values.

source

pub fn header_names(&self) -> Names<'_>

An iterator visiting all header names in arbitrary order.

source

pub fn header_values(&self) -> Values<'_>

An iterator visiting all header values in arbitrary order.

source

pub fn ext<T: Send + Sync + 'static>(&self) -> Option<&T>

Get a response scoped extension value.

source

pub fn insert_ext<T: Send + Sync + 'static>(&mut self, val: T)

Set a response scoped extension value.

source

pub fn content_type(&self) -> Option<Mime>

Get the response content type as a Mime.

Gets the Content-Type header and parses it to a Mime type.

Read more on MDN

§Panics

This method will panic if an invalid MIME type was set as a header.

§Examples
use crux_http::http::mime;
let res = client.get("https://httpbin.org/json").await?;
assert_eq!(res.content_type(), Some(mime::JSON));
source

pub fn len(&self) -> Option<usize>

Get the length of the body stream, if it has been set.

This value is set when passing a fixed-size object into as the body. E.g. a string, or a buffer. Consumers of this API should check this value to decide whether to use Chunked encoding, or set the response length.

source

pub fn is_empty(&self) -> Option<bool>

Returns true if the set length of the body stream is zero, false otherwise.

source

pub fn set_body(&mut self, body: impl Into<Body>)

Set the body reader.

source

pub fn take_body(&mut self) -> Body

Take the response body as a Body.

This method can be called after the body has already been taken or read, but will return an empty Body.

Useful for adjusting the whole body, such as in middleware.

source

pub fn swap_body(&mut self, body: &mut Body)

Swaps the value of the body with another body, without deinitializing either one.

source

pub async fn body_bytes(&mut self) -> Result<Vec<u8>>

Reads the entire request body into a byte buffer.

This method can be called after the body has already been read, but will produce an empty buffer.

§Errors

Any I/O error encountered while reading the body is immediately returned as an Err.

§Examples
let mut res = client.get("https://httpbin.org/get").await?;
let bytes: Vec<u8> = res.body_bytes().await?;
source

pub async fn body_string(&mut self) -> Result<String>

Reads the entire response body into a string.

This method can be called after the body has already been read, but will produce an empty buffer.

§Encodings

If the “encoding” feature is enabled, this method tries to decode the body with the encoding that is specified in the Content-Type header. If the header does not specify an encoding, UTF-8 is assumed. If the “encoding” feature is disabled, Surf only supports reading UTF-8 response bodies. The “encoding” feature is enabled by default.

§Errors

Any I/O error encountered while reading the body is immediately returned as an Err.

If the body cannot be interpreted because the encoding is unsupported or incorrect, an Err is returned.

§Examples
let mut res = client.get("https://httpbin.org/get").await?;
let string: String = res.body_string().await?;
source

pub async fn body_json<T: DeserializeOwned>(&mut self) -> Result<T>

Reads and deserialized the entire request body from json.

§Errors

Any I/O error encountered while reading the body is immediately returned as an Err.

If the body cannot be interpreted as valid json for the target type T, an Err is returned.

§Examples
#[derive(Deserialize, Serialize)]
struct Ip {
    ip: String
}

let mut res = client.get("https://api.ipify.org?format=json").await?;
let Ip { ip } = res.body_json().await?;
source

pub async fn body_form<T: DeserializeOwned>(&mut self) -> Result<T>

Reads and deserialized the entire request body from form encoding.

§Errors

Any I/O error encountered while reading the body is immediately returned as an Err.

If the body cannot be interpreted as valid json for the target type T, an Err is returned.

§Examples
#[derive(Deserialize, Serialize)]
struct Body {
    apples: u32
}

let mut res = client.get("https://api.example.com/v1/response").await?;
let Body { apples } = res.body_form().await?;

Trait Implementations§

source§

impl AsMut<Headers> for ResponseAsync

source§

fn as_mut(&mut self) -> &mut Headers

Converts this type into a mutable reference of the (usually inferred) input type.
source§

impl AsMut<Response> for ResponseAsync

source§

fn as_mut(&mut self) -> &mut Response

Converts this type into a mutable reference of the (usually inferred) input type.
source§

impl AsRef<Headers> for ResponseAsync

source§

fn as_ref(&self) -> &Headers

Converts this type into a shared reference of the (usually inferred) input type.
source§

impl AsRef<Response> for ResponseAsync

source§

fn as_ref(&self) -> &Response

Converts this type into a shared reference of the (usually inferred) input type.
source§

impl AsyncRead for ResponseAsync

source§

fn poll_read( self: Pin<&mut Self>, cx: &mut Context<'_>, buf: &mut [u8], ) -> Poll<Result<usize, Error>>

Attempt to read from the AsyncRead into buf. Read more
§

fn poll_read_vectored( self: Pin<&mut Self>, cx: &mut Context<'_>, bufs: &mut [IoSliceMut<'_>], ) -> Poll<Result<usize, Error>>

Attempt to read from the AsyncRead into bufs using vectored IO operations. Read more
source§

impl Debug for ResponseAsync

source§

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

Formats the value using the given formatter. Read more
source§

impl From<HttpResponse> for ResponseAsync

source§

fn from(effect_response: HttpResponse) -> Self

Converts to this type from the input type.
source§

impl From<Response> for ResponseAsync

source§

fn from(response: Response) -> Self

Converts to this type from the input type.
source§

impl Index<&str> for ResponseAsync

source§

fn index(&self, name: &str) -> &HeaderValues

Returns a reference to the value corresponding to the supplied name.

§Panics

Panics if the name is not present in Response.

source§

type Output = HeaderValues

The returned type after indexing.
source§

impl Index<HeaderName> for ResponseAsync

source§

fn index(&self, name: HeaderName) -> &HeaderValues

Returns a reference to the value corresponding to the supplied name.

§Panics

Panics if the name is not present in Response.

source§

type Output = HeaderValues

The returned type after indexing.
source§

impl Into<Response> for ResponseAsync

source§

fn into(self) -> Response

Converts this type into the (usually inferred) input type.
source§

impl<'__pin> Unpin for ResponseAsync
where __Origin<'__pin>: Unpin,

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
§

impl<R> AsyncReadExt for R
where R: AsyncRead + ?Sized,

§

fn chain<R>(self, next: R) -> Chain<Self, R>
where Self: Sized, R: AsyncRead,

Creates an adaptor which will chain this stream with another. Read more
§

fn read<'a>(&'a mut self, buf: &'a mut [u8]) -> Read<'a, Self>
where Self: Unpin,

Tries to read some bytes directly into the given buf in asynchronous manner, returning a future type. Read more
§

fn read_vectored<'a>( &'a mut self, bufs: &'a mut [IoSliceMut<'a>], ) -> ReadVectored<'a, Self>
where Self: Unpin,

Creates a future which will read from the AsyncRead into bufs using vectored IO operations. Read more
§

fn read_exact<'a>(&'a mut self, buf: &'a mut [u8]) -> ReadExact<'a, Self>
where Self: Unpin,

Creates a future which will read exactly enough bytes to fill buf, returning an error if end of file (EOF) is hit sooner. Read more
§

fn read_to_end<'a>(&'a mut self, buf: &'a mut Vec<u8>) -> ReadToEnd<'a, Self>
where Self: Unpin,

Creates a future which will read all the bytes from this AsyncRead. Read more
§

fn read_to_string<'a>( &'a mut self, buf: &'a mut String, ) -> ReadToString<'a, Self>
where Self: Unpin,

Creates a future which will read all the bytes from this AsyncRead. Read more
§

fn split(self) -> (ReadHalf<Self>, WriteHalf<Self>)
where Self: Sized + AsyncWrite,

Helper method for splitting this read/write object into two halves. Read more
§

fn take(self, limit: u64) -> Take<Self>
where Self: Sized,

Creates an AsyncRead adapter which will read at most limit bytes from the underlying reader. Read more
§

impl<R> AsyncReadExt for R
where R: AsyncRead + ?Sized,

§

fn read<'a>(&'a mut self, buf: &'a mut [u8]) -> ReadFuture<'a, Self>
where Self: Unpin,

Reads some bytes from the byte stream. Read more
§

fn read_vectored<'a>( &'a mut self, bufs: &'a mut [IoSliceMut<'a>], ) -> ReadVectoredFuture<'a, Self>
where Self: Unpin,

Like [read()][AsyncReadExt::read()], except it reads into a slice of buffers. Read more
§

fn read_to_end<'a>( &'a mut self, buf: &'a mut Vec<u8>, ) -> ReadToEndFuture<'a, Self>
where Self: Unpin,

Reads the entire contents and appends them to a Vec. Read more
§

fn read_to_string<'a>( &'a mut self, buf: &'a mut String, ) -> ReadToStringFuture<'a, Self>
where Self: Unpin,

Reads the entire contents and appends them to a String. Read more
§

fn read_exact<'a>(&'a mut self, buf: &'a mut [u8]) -> ReadExactFuture<'a, Self>
where Self: Unpin,

Reads the exact number of bytes required to fill buf. Read more
§

fn take(self, limit: u64) -> Take<Self>
where Self: Sized,

Creates an adapter which will read at most limit bytes from it. Read more
§

fn bytes(self) -> Bytes<Self>
where Self: Sized,

Converts this [AsyncRead] into a [Stream] of bytes. Read more
§

fn chain<R>(self, next: R) -> Chain<Self, R>
where R: AsyncRead, Self: Sized,

Creates an adapter which will chain this stream with another. Read more
§

fn boxed_reader<'a>(self) -> Pin<Box<dyn AsyncRead + Send + 'a>>
where Self: Sized + Send + 'a,

Boxes the reader and changes its type to dyn AsyncRead + Send + 'a. 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.

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, U> TryFrom<U> for T
where U: Into<T>,

source§

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>,

source§

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