Skip to main content

RawResponse

Struct RawResponse 

Source
pub struct RawResponse { /* private fields */ }
Expand description

An in-memory HTTP response as returned by the shell, used in the middleware chain.

Implementations§

Source§

impl RawResponse

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::HTTP_11));
Source

pub fn header_all(&self, name: impl AsHeaderName) -> GetAll<'_, HeaderValue>

Get all values for a header name.

Source

pub fn header(&self, name: impl AsHeaderName) -> Option<&HeaderValue>

Get a header value by name (returns the first value for that name).

§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 AsHeaderName, ) -> Option<&mut HeaderValue>

Get a header value mutably.

Source

pub fn remove_header(&mut self, name: impl AsHeaderName) -> Option<HeaderValue>

Remove a header.

Source

pub fn insert_header( &mut self, name: impl IntoHeaderName, value: HeaderValue, ) -> Option<HeaderValue>

Insert an HTTP header, replacing any existing value.

Returns the previous value for that header name, if any.

Source

pub fn append_header( &mut self, name: impl IntoHeaderName, value: HeaderValue, ) -> bool

Append an HTTP header, keeping any existing values.

Returns true if the value was appended to an existing entry, false if it was the first value for that name.

Source

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

An iterator visiting all header (name, value) pairs in arbitrary order.

Source

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

An iterator visiting all header (name, value) pairs with mutable values.

Source

pub fn header_names(&self) -> Keys<'_, HeaderValue>

An iterator visiting all header names in arbitrary order.

Source

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

An iterator visiting all header values in arbitrary order.

Source

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

Get the response content type as a Mime.

§Examples
let res = client.get("https://httpbin.org/json").await?;
assert_eq!(res.content_type(), Some(mime::APPLICATION_JSON));
Source

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

Get the length of the body in bytes.

Source

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

Returns true if the body is empty.

Source

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

Reads the entire response body into a byte buffer, leaving it empty.

§Errors

This function currently never returns an error; the Result wrapper is kept for API consistency.

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

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

Reads the entire response body into a string.

§Errors

Returns an error if the body contains invalid UTF-8.

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

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

Reads and deserializes the entire response body from JSON.

§Errors

Returns an error if deserialisation fails.

§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()?;
Source

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

Reads and deserializes the entire response body from form encoding.

§Errors

Returns an error if deserialisation fails.

§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()?;

Trait Implementations§

Source§

impl AsMut<HeaderMap> for RawResponse

Source§

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

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

impl AsRef<HeaderMap> for RawResponse

Source§

fn as_ref(&self) -> &HeaderMap

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

impl Debug for RawResponse

Source§

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

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

impl Index<&str> for RawResponse

Source§

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

Returns a reference to the first header value for the given name.

§Panics

Panics if the name is not present in RawResponse.

Source§

type Output = HeaderValue

The returned type after indexing.
Source§

impl<'a> IntoIterator for &'a RawResponse

Source§

type Item = (&'a HeaderName, &'a HeaderValue)

The type of the elements being iterated over.
Source§

type IntoIter = Iter<'a, HeaderValue>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<'a> IntoIterator for &'a mut RawResponse

Source§

type Item = (&'a HeaderName, &'a mut HeaderValue)

The type of the elements being iterated over.
Source§

type IntoIter = IterMut<'a, HeaderValue>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl TryFrom<HttpResponse> for RawResponse

Source§

type Error = HttpError

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

fn try_from(r: HttpResponse) -> Result<Self>

Performs the conversion.

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.

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.