Struct ajars_surf::surf::Response
source · [−]pub struct Response { /* private fields */ }
Expand description
An HTTP response, returned by Request
.
Implementations
sourceimpl Response
impl Response
sourcepub fn status(&self) -> StatusCode
pub fn status(&self) -> StatusCode
Get the HTTP status code.
Examples
let res = surf::get("https://httpbin.org/get").await?;
assert_eq!(res.status(), 200);
sourcepub fn version(&self) -> Option<Version>
pub fn version(&self) -> Option<Version>
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));
sourcepub fn header(&self, name: impl Into<HeaderName>) -> Option<&HeaderValues>
pub fn header(&self, name: impl Into<HeaderName>) -> Option<&HeaderValues>
Get a header.
Examples
let res = surf::get("https://httpbin.org/get").await?;
assert!(res.header("Content-Length").is_some());
sourcepub fn header_mut(
&mut self,
name: impl Into<HeaderName>
) -> Option<&mut HeaderValues>
pub fn header_mut(
&mut self,
name: impl Into<HeaderName>
) -> Option<&mut HeaderValues>
Get an HTTP header mutably.
sourcepub fn remove_header(
&mut self,
name: impl Into<HeaderName>
) -> Option<HeaderValues>
pub fn remove_header(
&mut self,
name: impl Into<HeaderName>
) -> Option<HeaderValues>
Remove a header.
sourcepub fn insert_header(
&mut self,
key: impl Into<HeaderName>,
value: impl ToHeaderValues
)
pub fn insert_header(
&mut self,
key: impl Into<HeaderName>,
value: impl ToHeaderValues
)
Insert an HTTP header.
sourcepub fn append_header(
&mut self,
key: impl Into<HeaderName>,
value: impl ToHeaderValues
)
pub fn append_header(
&mut self,
key: impl Into<HeaderName>,
value: impl ToHeaderValues
)
Append an HTTP header.
sourcepub fn iter(&self) -> Iter<'_>ⓘNotable traits for Iter<'a>impl<'a> Iterator for Iter<'a> type Item = (&'a HeaderName, &'a HeaderValues);
pub fn iter(&self) -> Iter<'_>ⓘNotable traits for Iter<'a>impl<'a> Iterator for Iter<'a> type Item = (&'a HeaderName, &'a HeaderValues);
An iterator visiting all header pairs in arbitrary order.
sourcepub fn iter_mut(&mut self) -> IterMut<'_>ⓘNotable traits for IterMut<'a>impl<'a> Iterator for IterMut<'a> type Item = (&'a HeaderName, &'a mut HeaderValues);
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);
An iterator visiting all header pairs in arbitrary order, with mutable references to the values.
sourcepub fn header_names(&self) -> Names<'_>ⓘNotable traits for Names<'a>impl<'a> Iterator for Names<'a> type Item = &'a HeaderName;
pub fn header_names(&self) -> Names<'_>ⓘNotable traits for Names<'a>impl<'a> Iterator for Names<'a> type Item = &'a HeaderName;
An iterator visiting all header names in arbitrary order.
sourcepub fn header_values(&self) -> Values<'_>ⓘNotable traits for Values<'a>impl<'a> Iterator for Values<'a> type Item = &'a HeaderValue;
pub fn header_values(&self) -> Values<'_>ⓘNotable traits for Values<'a>impl<'a> Iterator for Values<'a> type Item = &'a HeaderValue;
An iterator visiting all header values in arbitrary order.
sourcepub fn ext<T>(&self) -> Option<&T> where
T: 'static + Send + Sync,
pub fn ext<T>(&self) -> Option<&T> where
T: 'static + Send + Sync,
Get a response scoped extension value.
sourcepub fn insert_ext<T>(&mut self, val: T) where
T: 'static + Send + Sync,
pub fn insert_ext<T>(&mut self, val: T) where
T: 'static + Send + Sync,
Set a response scoped extension value.
sourcepub fn content_type(&self) -> Option<Mime>
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.
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));
sourcepub fn len(&self) -> Option<usize>
pub fn len(&self) -> Option<usize>
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.
sourcepub fn is_empty(&self) -> Option<bool>
pub fn is_empty(&self) -> Option<bool>
Returns true
if the set length of the body stream is zero, false
otherwise.
sourcepub fn take_body(&mut self) -> Body
pub fn take_body(&mut self) -> Body
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.
sourcepub fn swap_body(&mut self, body: &mut Body)
pub fn swap_body(&mut self, body: &mut Body)
Swaps the value of the body with another body, without deinitializing either one.
sourcepub async fn body_bytes(&'_ mut self) -> Result<Vec<u8, Global>, Error>
pub async fn body_bytes(&'_ mut self) -> Result<Vec<u8, Global>, Error>
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?;
sourcepub async fn body_string(&'_ mut self) -> Result<String, Error>
pub async fn body_string(&'_ mut self) -> Result<String, Error>
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 mut res = surf::get("https://httpbin.org/get").await?;
let string: String = res.body_string().await?;
sourcepub async fn body_json<T>(&'_ mut self) -> Result<T, Error> where
T: DeserializeOwned,
pub async fn body_json<T>(&'_ mut self) -> Result<T, Error> where
T: DeserializeOwned,
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?;
sourcepub async fn body_form<T>(&'_ mut self) -> Result<T, Error> where
T: DeserializeOwned,
pub async fn body_form<T>(&'_ mut self) -> Result<T, Error> where
T: DeserializeOwned,
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
sourceimpl AsyncBufRead for Response
impl AsyncBufRead for Response
sourceimpl AsyncRead for Response
impl AsyncRead for Response
sourceimpl<'_> Index<&'_ str> for Response
impl<'_> Index<&'_ str> for Response
sourcefn index(&self, name: &str) -> &HeaderValues
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
type Output = HeaderValues
The returned type after indexing.
sourceimpl Index<HeaderName> for Response
impl Index<HeaderName> for Response
sourcefn index(&self, name: HeaderName) -> &HeaderValues
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
type Output = HeaderValues
The returned type after indexing.
impl<'__pin> Unpin for Response where
__Origin<'__pin>: Unpin,
Auto Trait Implementations
impl !RefUnwindSafe for Response
impl Send for Response
impl Sync for Response
impl !UnwindSafe for Response
Blanket Implementations
impl<R> AsyncBufReadExt for R where
R: AsyncBufRead + ?Sized,
impl<R> AsyncBufReadExt for R where
R: AsyncBufRead + ?Sized,
fn fill_buf(&mut self) -> FillBuf<'_, Self> where
Self: Unpin,
fn fill_buf(&mut self) -> FillBuf<'_, Self> where
Self: Unpin,
Returns the contents of the internal buffer, filling it with more data if empty. Read more
fn read_until(
&'a mut self,
byte: u8,
buf: &'a mut Vec<u8, Global>
) -> ReadUntilFuture<'a, Self> where
Self: Unpin,
fn read_until(
&'a mut self,
byte: u8,
buf: &'a mut Vec<u8, Global>
) -> ReadUntilFuture<'a, Self> where
Self: Unpin,
Reads all bytes and appends them into buf
until the delimiter byte
or EOF is found. Read more
fn read_line(&'a mut self, buf: &'a mut String) -> ReadLineFuture<'a, Self> where
Self: Unpin,
fn read_line(&'a mut self, buf: &'a mut String) -> ReadLineFuture<'a, Self> where
Self: Unpin,
Reads all bytes and appends them into buf
until a newline (the 0xA byte) or EOF is found. Read more
impl<R> AsyncBufReadExt for R where
R: AsyncBufRead + ?Sized,
impl<R> AsyncBufReadExt for R where
R: AsyncBufRead + ?Sized,
fn fill_buf(&mut self) -> FillBuf<'_, Self> where
Self: Unpin,
fn fill_buf(&mut self) -> FillBuf<'_, Self> where
Self: Unpin,
Creates a future which will wait for a non-empty buffer to be available from this I/O object or EOF to be reached. Read more
fn consume_unpin(&mut self, amt: usize) where
Self: Unpin,
fn consume_unpin(&mut self, amt: usize) where
Self: Unpin,
fn read_until(
&'a mut self,
byte: u8,
buf: &'a mut Vec<u8, Global>
) -> ReadUntil<'a, Self> where
Self: Unpin,
fn read_until(
&'a mut self,
byte: u8,
buf: &'a mut Vec<u8, Global>
) -> ReadUntil<'a, Self> where
Self: Unpin,
Creates a future which will read all the bytes associated with this I/O
object into buf
until the delimiter byte
or EOF is reached.
This method is the async equivalent to BufRead::read_until
. Read more
fn read_line(&'a mut self, buf: &'a mut String) -> ReadLine<'a, Self> where
Self: Unpin,
fn read_line(&'a mut self, buf: &'a mut String) -> ReadLine<'a, Self> where
Self: Unpin,
Creates a future which will read all the bytes associated with this I/O
object into buf
until a newline (the 0xA byte) or EOF is reached,
This method is the async equivalent to BufRead::read_line
. Read more
fn lines(self) -> Lines<Self>
fn lines(self) -> Lines<Self>
Returns a stream over the lines of this reader.
This method is the async equivalent to BufRead::lines
. Read more
impl<R> AsyncReadExt for R where
R: AsyncRead + ?Sized,
impl<R> AsyncReadExt for R where
R: AsyncRead + ?Sized,
fn read(&'a mut self, buf: &'a mut [u8]) -> ReadFuture<'a, Self> where
Self: Unpin,
fn read(&'a mut self, buf: &'a mut [u8]) -> ReadFuture<'a, Self> where
Self: Unpin,
Reads some bytes from the byte stream. Read more
fn read_vectored(
&'a mut self,
bufs: &'a mut [IoSliceMut<'a>]
) -> ReadVectoredFuture<'a, Self> where
Self: Unpin,
fn read_vectored(
&'a mut self,
bufs: &'a mut [IoSliceMut<'a>]
) -> ReadVectoredFuture<'a, Self> where
Self: Unpin,
Like [read()
][AsyncReadExt::read()
], except it reads into a slice of buffers. Read more
fn read_to_end(
&'a mut self,
buf: &'a mut Vec<u8, Global>
) -> ReadToEndFuture<'a, Self> where
Self: Unpin,
fn read_to_end(
&'a mut self,
buf: &'a mut Vec<u8, Global>
) -> ReadToEndFuture<'a, Self> where
Self: Unpin,
fn read_to_string(
&'a mut self,
buf: &'a mut String
) -> ReadToStringFuture<'a, Self> where
Self: Unpin,
fn read_to_string(
&'a mut self,
buf: &'a mut String
) -> ReadToStringFuture<'a, Self> where
Self: Unpin,
fn read_exact(&'a mut self, buf: &'a mut [u8]) -> ReadExactFuture<'a, Self> where
Self: Unpin,
fn read_exact(&'a mut self, buf: &'a mut [u8]) -> ReadExactFuture<'a, Self> where
Self: Unpin,
Reads the exact number of bytes required to fill buf
. Read more
fn take(self, limit: u64) -> Take<Self>
fn take(self, limit: u64) -> Take<Self>
Creates an adapter which will read at most limit
bytes from it. Read more
impl<R> AsyncReadExt for R where
R: AsyncRead + ?Sized,
impl<R> AsyncReadExt for R where
R: AsyncRead + ?Sized,
fn chain<R>(self, next: R) -> Chain<Self, R> where
R: AsyncRead,
fn chain<R>(self, next: R) -> Chain<Self, R> where
R: AsyncRead,
Creates an adaptor which will chain this stream with another. Read more
fn read(&'a mut self, buf: &'a mut [u8]) -> Read<'a, Self> where
Self: Unpin,
fn read(&'a mut self, buf: &'a mut [u8]) -> Read<'a, Self> where
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,
fn read_vectored(
&'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,
fn read_exact(&'a mut self, buf: &'a mut [u8]) -> ReadExact<'a, Self> where
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, Global>
) -> ReadToEnd<'a, Self> where
Self: Unpin,
fn read_to_end(
&'a mut self,
buf: &'a mut Vec<u8, Global>
) -> ReadToEnd<'a, Self> where
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,
fn read_to_string(&'a mut self, buf: &'a mut String) -> ReadToString<'a, Self> where
Self: Unpin,
Creates a future which will read all the bytes from this AsyncRead
. Read more
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
impl<T> BufReadExt for T where
T: AsyncBufRead + ?Sized,
impl<T> BufReadExt for T where
T: AsyncBufRead + ?Sized,
fn read_until(
&'a mut self,
byte: u8,
buf: &'a mut Vec<u8, Global>
) -> ReadUntilFuture<'a, Self> where
Self: Unpin,
fn read_until(
&'a mut self,
byte: u8,
buf: &'a mut Vec<u8, Global>
) -> ReadUntilFuture<'a, Self> where
Self: Unpin,
Reads all bytes into buf
until the delimiter byte
or EOF is reached. Read more
fn read_line(&'a mut self, buf: &'a mut String) -> ReadLineFuture<'a, Self> where
Self: Unpin,
fn read_line(&'a mut self, buf: &'a mut String) -> ReadLineFuture<'a, Self> where
Self: Unpin,
Reads all bytes and appends them into buf
until a newline (the 0xA byte) is
reached. Read more
impl<T> ReadExt for T where
T: AsyncRead + ?Sized,
impl<T> ReadExt for T where
T: AsyncRead + ?Sized,
fn read(&'a mut self, buf: &'a mut [u8]) -> ReadFuture<'a, Self> where
Self: Unpin,
fn read(&'a mut self, buf: &'a mut [u8]) -> ReadFuture<'a, Self> where
Self: Unpin,
Reads some bytes from the byte stream. Read more
fn read_vectored(
&'a mut self,
bufs: &'a mut [IoSliceMut<'a>]
) -> ReadVectoredFuture<'a, Self> where
Self: Unpin,
fn read_vectored(
&'a mut self,
bufs: &'a mut [IoSliceMut<'a>]
) -> ReadVectoredFuture<'a, Self> where
Self: Unpin,
fn read_to_end(
&'a mut self,
buf: &'a mut Vec<u8, Global>
) -> ReadToEndFuture<'a, Self> where
Self: Unpin,
fn read_to_end(
&'a mut self,
buf: &'a mut Vec<u8, Global>
) -> ReadToEndFuture<'a, Self> where
Self: Unpin,
Reads all bytes from the byte stream. Read more
fn read_to_string(
&'a mut self,
buf: &'a mut String
) -> ReadToStringFuture<'a, Self> where
Self: Unpin,
fn read_to_string(
&'a mut self,
buf: &'a mut String
) -> ReadToStringFuture<'a, Self> where
Self: Unpin,
Reads all bytes from the byte stream and appends them into a string. Read more
fn read_exact(&'a mut self, buf: &'a mut [u8]) -> ReadExactFuture<'a, Self> where
Self: Unpin,
fn read_exact(&'a mut self, buf: &'a mut [u8]) -> ReadExactFuture<'a, Self> where
Self: Unpin,
Reads the exact number of bytes required to fill buf
. Read more
fn take(self, limit: u64) -> Take<Self>
fn take(self, limit: u64) -> Take<Self>
Creates an adaptor which will read at most limit
bytes from it. Read more