[−][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
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]
U: From<T>,
impl<T> From<T> for T
[src]
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 chain<R>(self, next: R) -> Chain<Self, R> where
R: AsyncRead,
[src]
R: AsyncRead,
fn copy_into<W>(self, writer: &mut W) -> CopyInto<Self, W> where
W: AsyncWrite + Unpin + ?Sized,
[src]
W: AsyncWrite + Unpin + ?Sized,
fn read(&'a mut self, buf: &'a mut [u8]) -> Read<'a, Self> where
Self: Unpin,
[src]
Self: Unpin,
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,
fn read_exact(&'a mut self, buf: &'a mut [u8]) -> ReadExact<'a, Self> where
Self: Unpin,
[src]
Self: Unpin,
fn read_to_end(&'a mut self, buf: &'a mut Vec<u8>) -> ReadToEnd<'a, Self> where
Self: Unpin,
[src]
Self: Unpin,
fn read_to_string(&'a mut self, buf: &'a mut String) -> ReadToString<'a, Self> where
Self: Unpin,
[src]
Self: Unpin,
fn split(self) -> (ReadHalf<Self>, WriteHalf<Self>) where
Self: AsyncWrite,
[src]
Self: AsyncWrite,
fn take(self, limit: u64) -> Take<Self>
[src]
fn compat(self) -> Compat<Self> where
Self: Unpin,
[src]
Self: Unpin,