Struct crux_http::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
impl ResponseAsync
sourcepub fn status(&self) -> StatusCode
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);
sourcepub fn version(&self) -> Option<Version>
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));
sourcepub fn header(&self, name: impl Into<HeaderName>) -> Option<&HeaderValues>
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());
sourcepub fn header_mut(
&mut self,
name: impl Into<HeaderName>,
) -> Option<&mut HeaderValues>
pub fn header_mut( &mut self, name: impl Into<HeaderName>, ) -> Option<&mut HeaderValues>
Get an HTTP header mutably.
sourcepub fn remove_header(
&mut self,
name: impl Into<HeaderName>,
) -> Option<HeaderValues>
pub fn remove_header( &mut self, name: impl Into<HeaderName>, ) -> Option<HeaderValues>
Remove a header.
sourcepub fn insert_header(
&mut self,
key: impl Into<HeaderName>,
value: impl ToHeaderValues,
)
pub fn insert_header( &mut self, key: impl Into<HeaderName>, value: impl ToHeaderValues, )
Insert an HTTP header.
sourcepub fn append_header(
&mut self,
key: impl Into<HeaderName>,
value: impl ToHeaderValues,
)
pub fn append_header( &mut self, key: impl Into<HeaderName>, value: impl ToHeaderValues, )
Append an HTTP header.
sourcepub fn iter_mut(&mut self) -> IterMut<'_>
pub fn iter_mut(&mut self) -> IterMut<'_>
An iterator visiting all header pairs in arbitrary order, with mutable references to the values.
sourcepub fn header_names(&self) -> Names<'_>
pub fn header_names(&self) -> Names<'_>
An iterator visiting all header names in arbitrary order.
sourcepub fn header_values(&self) -> Values<'_>
pub fn header_values(&self) -> Values<'_>
An iterator visiting all header values in arbitrary order.
sourcepub fn ext<T: Send + Sync + 'static>(&self) -> Option<&T>
pub fn ext<T: Send + Sync + 'static>(&self) -> Option<&T>
Get a response scoped extension value.
sourcepub fn insert_ext<T: Send + Sync + 'static>(&mut self, val: T)
pub fn insert_ext<T: Send + Sync + 'static>(&mut self, val: T)
Set a response scoped extension value.
sourcepub fn content_type(&self) -> Option<Mime>
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.
§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));
sourcepub fn len(&self) -> Option<usize>
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.
sourcepub fn is_empty(&self) -> Option<bool>
pub fn is_empty(&self) -> Option<bool>
Returns true
if the set length of the body stream is zero, false
otherwise.
sourcepub fn take_body(&mut self) -> Body
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.
sourcepub fn swap_body(&mut self, body: &mut Body)
pub fn swap_body(&mut self, body: &mut Body)
Swaps the value of the body with another body, without deinitializing either one.
sourcepub async fn body_bytes(&mut self) -> Result<Vec<u8>>
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?;
sourcepub async fn body_string(&mut self) -> Result<String>
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?;
sourcepub async fn body_json<T: DeserializeOwned>(&mut self) -> Result<T>
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?;
sourcepub async fn body_form<T: DeserializeOwned>(&mut self) -> Result<T>
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
impl AsMut<Headers> for ResponseAsync
source§impl AsMut<Response> for ResponseAsync
impl AsMut<Response> for ResponseAsync
source§impl AsRef<Headers> for ResponseAsync
impl AsRef<Headers> for ResponseAsync
source§impl AsRef<Response> for ResponseAsync
impl AsRef<Response> for ResponseAsync
source§impl AsyncRead for ResponseAsync
impl AsyncRead for ResponseAsync
source§impl Debug for ResponseAsync
impl Debug for ResponseAsync
source§impl From<HttpResponse> for ResponseAsync
impl From<HttpResponse> for ResponseAsync
source§fn from(effect_response: HttpResponse) -> Self
fn from(effect_response: HttpResponse) -> Self
source§impl From<Response> for ResponseAsync
impl From<Response> for ResponseAsync
source§impl Index<&str> for ResponseAsync
impl Index<&str> for ResponseAsync
source§impl Index<HeaderName> for ResponseAsync
impl Index<HeaderName> for ResponseAsync
source§impl Into<Response> for ResponseAsync
impl Into<Response> for ResponseAsync
impl<'__pin> Unpin for ResponseAsyncwhere
__Origin<'__pin>: Unpin,
Auto Trait Implementations§
impl Freeze for ResponseAsync
impl !RefUnwindSafe for ResponseAsync
impl Send for ResponseAsync
impl Sync for ResponseAsync
impl !UnwindSafe for ResponseAsync
Blanket Implementations§
§impl<R> AsyncReadExt for Rwhere
R: AsyncRead + ?Sized,
impl<R> AsyncReadExt for Rwhere
R: AsyncRead + ?Sized,
§fn chain<R>(self, next: R) -> Chain<Self, R>where
Self: Sized,
R: AsyncRead,
fn chain<R>(self, next: R) -> Chain<Self, R>where
Self: Sized,
R: AsyncRead,
§fn read<'a>(&'a mut self, buf: &'a mut [u8]) -> Read<'a, Self>where
Self: Unpin,
fn read<'a>(&'a mut self, buf: &'a mut [u8]) -> Read<'a, Self>where
Self: Unpin,
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,
fn read_vectored<'a>(
&'a mut self,
bufs: &'a mut [IoSliceMut<'a>],
) -> ReadVectored<'a, Self>where
Self: Unpin,
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,
fn read_exact<'a>(&'a mut self, buf: &'a mut [u8]) -> ReadExact<'a, Self>where
Self: Unpin,
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,
fn read_to_end<'a>(&'a mut self, buf: &'a mut Vec<u8>) -> ReadToEnd<'a, Self>where
Self: Unpin,
AsyncRead
. Read more§fn read_to_string<'a>(
&'a mut self,
buf: &'a mut String,
) -> ReadToString<'a, Self>where
Self: Unpin,
fn read_to_string<'a>(
&'a mut self,
buf: &'a mut String,
) -> ReadToString<'a, Self>where
Self: Unpin,
AsyncRead
. Read more§impl<R> AsyncReadExt for Rwhere
R: AsyncRead + ?Sized,
impl<R> AsyncReadExt for Rwhere
R: AsyncRead + ?Sized,
§fn read<'a>(&'a mut self, buf: &'a mut [u8]) -> ReadFuture<'a, Self>where
Self: Unpin,
fn read<'a>(&'a mut self, buf: &'a mut [u8]) -> ReadFuture<'a, Self>where
Self: Unpin,
§fn read_vectored<'a>(
&'a mut self,
bufs: &'a mut [IoSliceMut<'a>],
) -> ReadVectoredFuture<'a, Self>where
Self: Unpin,
fn read_vectored<'a>(
&'a mut self,
bufs: &'a mut [IoSliceMut<'a>],
) -> ReadVectoredFuture<'a, Self>where
Self: Unpin,
§fn read_to_end<'a>(
&'a mut self,
buf: &'a mut Vec<u8>,
) -> ReadToEndFuture<'a, Self>where
Self: Unpin,
fn read_to_end<'a>(
&'a mut self,
buf: &'a mut Vec<u8>,
) -> ReadToEndFuture<'a, Self>where
Self: Unpin,
§fn read_to_string<'a>(
&'a mut self,
buf: &'a mut String,
) -> ReadToStringFuture<'a, Self>where
Self: Unpin,
fn read_to_string<'a>(
&'a mut self,
buf: &'a mut String,
) -> ReadToStringFuture<'a, Self>where
Self: Unpin,
§fn read_exact<'a>(&'a mut self, buf: &'a mut [u8]) -> ReadExactFuture<'a, Self>where
Self: Unpin,
fn read_exact<'a>(&'a mut self, buf: &'a mut [u8]) -> ReadExactFuture<'a, Self>where
Self: Unpin,
buf
. Read more§fn take(self, limit: u64) -> Take<Self>where
Self: Sized,
fn take(self, limit: u64) -> Take<Self>where
Self: Sized,
limit
bytes from it. Read more