Skip to main content

Response

Struct Response 

Source
pub struct Response<Body> { /* private fields */ }
Expand description

An HTTP Response that will be passed to an app’s update function.

Implementations§

Source§

impl<Body> Response<Body>

Source

pub fn status(&self) -> StatusCode

Get the HTTP status code.

§Examples
assert_eq!(res.status(), 200);
Source

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

Get the HTTP protocol version.

§Examples
use crux_http::http::Version;
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.

§Examples
assert!(res.header("Content-Length").is_some());
Source

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

Get an HTTP header 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
assert_eq!(res.content_type(), Some(mime::APPLICATION_JSON));
Source

pub fn body(&self) -> Option<&Body>

Source

pub fn take_body(&mut self) -> Option<Body>

Source

pub fn with_body<NewBody>(self, body: NewBody) -> Response<NewBody>

Source§

impl Response<Vec<u8>>

Source

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

Reads the entire request body into a byte buffer.

§Errors

Returns an error if the body has already been taken.

§Examples
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 has already been taken or if it contains invalid UTF-8.

§Examples
let string: String = res.body_string()?;
assert_eq!(string, "hello");
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 the body has already been taken or if deserialisation fails.

§Examples
#[derive(Deserialize, Serialize)]
struct Ip { ip: String }
let Ip { ip } = res.body_json()?;
assert_eq!(ip, "127.0.0.1");

Trait Implementations§

Source§

impl<Body> AsMut<HeaderMap> for Response<Body>

Source§

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

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

impl<Body> AsRef<HeaderMap> for Response<Body>

Source§

fn as_ref(&self) -> &HeaderMap

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

impl<Body: Clone> Clone for Response<Body>

Source§

fn clone(&self) -> Response<Body>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<Body> Debug for Response<Body>

Source§

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

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

impl<'de, Body> Deserialize<'de> for Response<Body>
where Body: Deserialize<'de>,

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl<Body> Index<&str> for Response<Body>

Source§

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

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

§Panics

Panics if the name is not present in Response.

Source§

type Output = HeaderValue

The returned type after indexing.
Source§

impl<'a, Body> IntoIterator for &'a Response<Body>

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, Body> IntoIterator for &'a mut Response<Body>

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<Body> PartialEq for Response<Body>
where Body: PartialEq,

Source§

fn eq(&self, other: &Self) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<Body> Serialize for Response<Body>
where Body: Serialize,

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl<Body> TryFrom<Response<Body>> for Response<Body>

Source§

type Error = ()

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

fn try_from(res: Response<Body>) -> Result<Self, ()>

Performs the conversion.
Source§

impl<Body> Eq for Response<Body>
where Body: Eq,

Auto Trait Implementations§

§

impl<Body> Freeze for Response<Body>
where Body: Freeze,

§

impl<Body> RefUnwindSafe for Response<Body>
where Body: RefUnwindSafe,

§

impl<Body> Send for Response<Body>
where Body: Send,

§

impl<Body> Sync for Response<Body>
where Body: Sync,

§

impl<Body> Unpin for Response<Body>
where Body: Unpin,

§

impl<Body> UnsafeUnpin for Response<Body>
where Body: UnsafeUnpin,

§

impl<Body> UnwindSafe for Response<Body>
where Body: UnwindSafe,

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,