[][src]Struct surf::Response

pub struct Response { /* fields omitted */ }

An HTTP response, returned by Request.

Methods

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) -> Version[src]

Get the HTTP protocol version.

Examples

use surf::http::version::Version;

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

pub fn header(&self, key: &'static str) -> Option<&str>[src]

Get a header.

Examples

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

pub fn headers(&mut self) -> Headers[src]

Get all headers.

Examples

let mut res = surf::post("https://httpbin.org/get").await?;
for (name, value) in res.headers() {
    println!("{}: {}", name, value);
}

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

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

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, Exception>[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.

Errors

Any I/O error encountered while reading the body is immediately returned as an Err.

If the body cannot be interpreted as valid UTF-8, 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, Exception>
[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 Debug for Response[src]

impl AsyncRead for Response[src]

Auto Trait Implementations

impl Send for Response

impl !Sync for Response

impl Unpin for Response

impl !UnwindSafe for Response

impl !RefUnwindSafe for Response

Blanket Implementations

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

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

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<T> Borrow<T> for T where
    T: ?Sized
[src]

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

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

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