Struct surf::Response [−][src]
pub struct Response { /* fields omitted */ }
Expand description
An HTTP response, returned by Request
.
Implementations
Get the HTTP status code.
Examples
let res = surf::get("https://httpbin.org/get").await?;
assert_eq!(res.status(), 200);
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));
Get a header.
Examples
let res = surf::get("https://httpbin.org/get").await?;
assert!(res.header("Content-Length").is_some());
Get an HTTP header mutably.
Remove a header.
Insert an HTTP header.
Append an HTTP header.
An iterator visiting all header pairs in arbitrary order, with mutable references to the values.
An iterator visiting all header names in arbitrary order.
An iterator visiting all header values in arbitrary order.
Get a response scoped extension value.
Set a response scoped extension value.
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));
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.
Returns true
if the set length of the body stream is zero, false
otherwise.
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.
Swaps the value of the body with another body, without deinitializing either one.
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?;
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?;
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?;
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
Attempt to return the contents of the internal buffer, filling it with more data from the inner reader if it is empty. Read more
Attempt to read from the AsyncRead
into buf
. Read more
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.
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.
Auto Trait Implementations
impl !RefUnwindSafe 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,
Returns the contents of the internal buffer, filling it with more data if empty. Read more
Reads all bytes and appends them into buf
until the delimiter byte
or EOF is found. Read more
Reads all bytes and appends them into buf
until a newline (the 0xA byte) or EOF is found. Read more
Returns a stream over the lines of this byte stream. Read more
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
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
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
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,
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
Creates an adapter which will read at most limit
bytes from it. Read more
Creates an adaptor which will chain this stream with another. Read more
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
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
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
Helper method for splitting this read/write object into two halves. Read more
Mutably borrows from an owned value. Read more
Instruments this type with the provided Span
, returning an
Instrumented
wrapper. Read more
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,
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
Reads the exact number of bytes required to fill buf
. Read more
Creates an adaptor which will read at most limit
bytes from it. Read more
Creates a “by reference” adaptor for this instance of Read
. Read more
Transforms this Read
instance to a Stream
over its bytes. Read more
pub fn vzip(self) -> V
Attaches the provided Subscriber
to this type, returning a
WithDispatch
wrapper. Read more
Attaches the current default Subscriber
to this type, returning a
WithDispatch
wrapper. Read more
Attaches the provided Subscriber
to this type, returning a
WithDispatch
wrapper. Read more
Attaches the current default Subscriber
to this type, returning a
WithDispatch
wrapper. Read more