Struct mio_surf::Response[][src]

pub struct Response { /* fields omitted */ }

An HTTP response, returned by Request.

Implementations

impl Response[src]

pub fn status(&self) -> StatusCode[src]

Get the HTTP status code.

Examples

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

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

Get the HTTP protocol version.

Examples

use surf::http::Version;

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

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

Get a header.

Examples

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

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

Get an HTTP header mutably.

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

Remove a header.

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

Insert an HTTP header.

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

Append an HTTP header.

#[must_use]pub fn iter(&self) -> Iter<'_>

Notable traits for Iter<'a>

impl<'a> Iterator for Iter<'a> type Item = (&'a HeaderName, &'a HeaderValues);
[src]

An iterator visiting all header pairs in arbitrary order.

#[must_use]pub fn iter_mut(&mut self) -> IterMut<'_>

Notable traits for IterMut<'a>

impl<'a> Iterator for IterMut<'a> type Item = (&'a HeaderName, &'a mut HeaderValues);
[src]

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

#[must_use]pub fn header_names(&self) -> Names<'_>

Notable traits for Names<'a>

impl<'a> Iterator for Names<'a> type Item = &'a HeaderName;
[src]

An iterator visiting all header names in arbitrary order.

#[must_use]pub fn header_values(&self) -> Values<'_>

Notable traits for Values<'a>

impl<'a> Iterator for Values<'a> type Item = &'a HeaderValue;
[src]

An iterator visiting all header values in arbitrary order.

#[must_use]pub fn ext<T: Send + Sync + 'static>(&self) -> Option<&T>[src]

Get a response scoped extension value.

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

Set a response scoped extension value.

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

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 surf::http::mime;
let res = surf::get("https://httpbin.org/json").await?;
assert_eq!(res.content_type(), Some(mime::JSON));

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

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.

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

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

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

Set the body reader.

pub fn take_body(&mut self) -> Body[src]

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.

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

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

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

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 = surf::get("https://httpbin.org/get").await?;
let bytes: Vec<u8> = res.body_bytes().await?;

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

Reads the entire request 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 = surf::get("https://httpbin.org/get").await?;
let string: String = res.body_string().await?;

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

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 = surf::get("https://api.ipify.org?format=json").await?;
let Ip { ip } = res.body_json().await?;

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

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 = surf::get("https://api.example.com/v1/response").await?;
let Body { apples } = res.body_form().await?;

Trait Implementations

impl AsMut<Response> for Response[src]

impl AsRef<Response> for Response[src]

impl AsyncBufRead for Response[src]

impl AsyncRead for Response[src]

impl Debug for Response[src]

impl From<Response> for Response[src]

impl Index<&'_ str> for Response[src]

type Output = HeaderValues

The returned type after indexing.

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

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

Panics

Panics if the name is not present in Response.

impl Index<HeaderName> for Response[src]

type Output = HeaderValues

The returned type after indexing.

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

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

Panics

Panics if the name is not present in Response.

impl Into<Response> for Response[src]

impl<'__pin> Unpin for Response where
    __Origin<'__pin>: Unpin
[src]

Auto Trait Implementations

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<R> AsyncBufReadExt for R where
    R: AsyncBufRead + ?Sized
[src]

impl<R> AsyncBufReadExt for R where
    R: AsyncBufRead + ?Sized

impl<R> AsyncReadExt for R where
    R: AsyncRead + ?Sized
[src]

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

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> BufReadExt for T where
    T: AsyncBufRead + ?Sized
[src]

impl<T> From<T> for T[src]

impl<T> Instrument for T[src]

impl<T> Instrument for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> ReadExt for T where
    T: AsyncRead + ?Sized
[src]

impl<T> Same<T> for T

type Output = T

Should always be Self

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.

impl<V, T> VZip<V> for T where
    V: MultiLane<T>, 

impl<T> WithSubscriber for T[src]