[−][src]Struct surf::Response
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.
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]
&'_ mut self
) -> Result<T, Exception>
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]
fn poll_read(
self: Pin<&mut Self>,
cx: &mut Context,
buf: &mut [u8]
) -> Poll<Result<usize, Error>>
[src]
self: Pin<&mut Self>,
cx: &mut Context,
buf: &mut [u8]
) -> Poll<Result<usize, Error>>
unsafe fn initializer(&self) -> Initializer
[src]
Determines if this AsyncRead
er can work with buffers of uninitialized memory. Read more
fn poll_read_vectored(
self: Pin<&mut Self>,
cx: &mut Context,
bufs: &mut [IoSliceMut]
) -> Poll<Result<usize, Error>>
[src]
self: Pin<&mut Self>,
cx: &mut Context,
bufs: &mut [IoSliceMut]
) -> Poll<Result<usize, Error>>
Attempt to read from the AsyncRead
into bufs
using vectored IO operations. Read more
Auto Trait Implementations
impl Send for Response
impl Unpin for Response
impl !Sync for Response
impl !UnwindSafe for Response
impl !RefUnwindSafe for Response
Blanket Implementations
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,
type Error = <U as TryFrom<T>>::Error
The type returned in the event of a conversion error.
fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>
[src]
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<R> AsyncReadExt for R where
R: AsyncRead + ?Sized,
[src]
R: AsyncRead + ?Sized,
fn copy_into<W>(self, writer: &mut W) -> CopyInto<Self, W> where
W: AsyncWrite + Unpin + ?Sized,
[src]
W: AsyncWrite + Unpin + ?Sized,
Creates a future which copies all the bytes from one object to another. Read more
fn read(&'a mut self, buf: &'a mut [u8]) -> Read<'a, Self> where
Self: Unpin,
[src]
Self: Unpin,
Tries to read some bytes directly into the given buf
in asynchronous manner, returning a future type. Read more
fn read_vectored(
&'a mut self,
bufs: &'a mut [IoSliceMut<'a>]
) -> ReadVectored<'a, Self> where
Self: Unpin,
[src]
&'a mut self,
bufs: &'a mut [IoSliceMut<'a>]
) -> ReadVectored<'a, Self> where
Self: Unpin,
Creates a future which will read from the AsyncRead
into bufs
using vectored IO operations. Read more
fn read_exact(&'a mut self, buf: &'a mut [u8]) -> ReadExact<'a, Self> where
Self: Unpin,
[src]
Self: Unpin,
Creates a future which will read exactly enough bytes to fill buf
, returning an error if end of file (EOF) is hit sooner. Read more
fn read_to_end(&'a mut self, buf: &'a mut Vec<u8>) -> ReadToEnd<'a, Self> where
Self: Unpin,
[src]
Self: Unpin,
Creates a future which will read all the bytes from this AsyncRead
. Read more
fn read_to_string(&'a mut self, buf: &'a mut String) -> ReadToString<'a, Self> where
Self: Unpin,
[src]
Self: Unpin,
Creates a future which will read all the bytes from this AsyncRead
. Read more
fn split(self) -> (ReadHalf<Self>, WriteHalf<Self>) where
Self: AsyncWrite,
[src]
Self: AsyncWrite,
Helper method for splitting this read/write object into two halves. Read more
fn compat(self) -> Compat<Self> where
Self: Unpin,
[src]
Self: Unpin,
Wraps an [AsyncRead
] in a compatibility wrapper that allows it to be used as a futures 0.1 / tokio-io 0.1 AsyncRead
. If the wrapped type implements [AsyncWrite
] as well, the result will also implement the futures 0.1 / tokio 0.1 AsyncWrite
trait. Read more