Struct crux_http::Response

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

An HTTP Response that will be passed to in a message to an apps 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::Http1_1));
source

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

Get a header.

§Examples
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 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;
assert_eq!(res.content_type(), Some(mime::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.

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 bytes: Vec<u8> = res.body_bytes()?;
source

pub 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 string: String = res.body_string()?;
assert_eq!(string, "hello");
source

pub 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 Ip { ip } = res.body_json()?;
assert_eq!(ip, "127.0.0.1");

Trait Implementations§

source§

impl<Body> AsMut<Headers> for Response<Body>

source§

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

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

impl<Body> AsRef<Headers> for Response<Body>

source§

fn as_ref(&self) -> &Headers

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 copy 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) -> &HeaderValues

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

§Panics

Panics if the name is not present in Response.

§

type Output = HeaderValues

The returned type after indexing.
source§

impl<Body> Index<HeaderName> for Response<Body>

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.

§

type Output = HeaderValues

The returned type after indexing.
source§

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

source§

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

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

This method 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> Eq for Response<Body>
where Body: Eq,

Auto Trait Implementations§

§

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> 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> 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> Serialize for T
where T: Serialize + ?Sized,

source§

fn erased_serialize(&self, serializer: &mut dyn Serializer) -> Result<(), Error>

source§

fn do_erased_serialize( &self, serializer: &mut dyn Serializer ) -> Result<(), ErrorImpl>

source§

impl<T> ToOwned for T
where T: Clone,

§

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

§

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

source§

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