[−][src]Struct surf::Response
An HTTP response, returned by Request
.
Implementations
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) -> Option<Version>
[src]
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));
pub fn header(&self, name: impl Into<HeaderName>) -> Option<&HeaderValues>
[src]
Get a header.
Examples
let res = surf::get("https://httpbin.org/get").await?; assert!(res.header("Content-Length").is_some());
pub fn header_mut(
&mut self,
name: impl Into<HeaderName>
) -> Option<&mut HeaderValues>
[src]
&mut self,
name: impl Into<HeaderName>
) -> Option<&mut HeaderValues>
Get an HTTP header mutably.
pub fn remove_header(
&mut self,
name: impl Into<HeaderName>
) -> Option<HeaderValues>
[src]
&mut self,
name: impl Into<HeaderName>
) -> Option<HeaderValues>
Remove a header.
pub fn insert_header(
&mut self,
key: impl Into<HeaderName>,
value: impl ToHeaderValues
)
[src]
&mut self,
key: impl Into<HeaderName>,
value: impl ToHeaderValues
)
Insert an HTTP header.
pub fn append_header(
&mut self,
key: impl Into<HeaderName>,
value: impl ToHeaderValues
)
[src]
&mut self,
key: impl Into<HeaderName>,
value: impl ToHeaderValues
)
Append an HTTP header.
#[must_use]pub fn iter(&self) -> Iter<'_>ⓘNotable traits for Iter<'a>
impl<'a> Iterator for Iter<'a> type Item = (&'a HeaderName, &'a HeaderValues);
[src]
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.
#[must_use]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);
[src]
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.
#[must_use]pub fn header_names(&self) -> Names<'_>ⓘNotable traits for Names<'a>
impl<'a> Iterator for Names<'a> type Item = &'a HeaderName;
[src]
Notable traits for Names<'a>
impl<'a> Iterator for Names<'a> type Item = &'a HeaderName;
An iterator visiting all header names in arbitrary order.
#[must_use]pub fn header_values(&self) -> Values<'_>ⓘNotable traits for Values<'a>
impl<'a> Iterator for Values<'a> type Item = &'a HeaderValue;
[src]
Notable traits for Values<'a>
impl<'a> Iterator for Values<'a> type Item = &'a HeaderValue;
An iterator visiting all header values in arbitrary order.
#[must_use]pub fn ext<T: Send + Sync + 'static>(&self) -> Option<&T>
[src]
Get a response scoped extension value.
pub fn insert_ext<T: Send + Sync + 'static>(&mut self, val: T)
[src]
Set a response scoped extension value.
pub fn content_type(&self) -> Option<Mime>
[src]
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));
pub fn len(&self) -> Option<usize>
[src]
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.
pub fn is_empty(&self) -> Option<bool>
[src]
Returns true
if the set length of the body stream is zero, false
otherwise.
pub fn set_body(&mut self, body: impl Into<Body>)
[src]
Set the body reader.
pub fn take_body(&mut self) -> Body
[src]
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.
pub fn swap_body(&mut self, body: &mut Body)
[src]
Swaps the value of the body with another body, without deinitializing either one.
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>
[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.
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?;
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>
[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 AsMut<Response> for Response
[src]
impl AsRef<Response> for Response
[src]
impl AsyncBufRead for Response
[src]
fn poll_fill_buf(
self: Pin<&mut Self>,
cx: &mut Context<'_>
) -> Poll<Result<&[u8]>>
[src]
self: Pin<&mut Self>,
cx: &mut Context<'_>
) -> Poll<Result<&[u8]>>
fn consume(self: Pin<&mut Self>, amt: usize)
[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>>
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>>
impl Debug for Response
[src]
impl From<Response> for Response
[src]
impl<'_> Index<&'_ str> for Response
[src]
type Output = HeaderValues
The returned type after indexing.
fn index(&self, name: &str) -> &HeaderValues
[src]
Returns a reference to the value corresponding to the supplied name.
Panics
Panics if the name is not present in Response
.
impl Index<HeaderName> for Response
[src]
type Output = HeaderValues
The returned type after indexing.
fn index(&self, name: HeaderName) -> &HeaderValues
[src]
Returns a reference to the value corresponding to the supplied name.
Panics
Panics if the name is not present in Response
.
impl Into<Response> for Response
[src]
impl<'__pin> Unpin for Response where
__Origin<'__pin>: Unpin,
[src]
__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<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<R> AsyncBufReadExt for R where
R: AsyncBufRead + ?Sized,
[src]
R: AsyncBufRead + ?Sized,
fn read_until(
&'a mut self,
byte: u8,
buf: &'a mut Vec<u8>
) -> ReadUntil<'a, Self> where
Self: Unpin,
[src]
&'a mut self,
byte: u8,
buf: &'a mut Vec<u8>
) -> ReadUntil<'a, Self> where
Self: Unpin,
fn read_line(&'a mut self, buf: &'a mut String) -> ReadLine<'a, Self> where
Self: Unpin,
[src]
Self: Unpin,
fn lines(self) -> Lines<Self>
[src]
impl<R> AsyncBufReadExt for R where
R: AsyncBufRead + ?Sized,
R: AsyncBufRead + ?Sized,
fn read_until(
&'a mut self,
byte: u8,
buf: &'a mut Vec<u8>
) -> ReadUntilFuture<'a, Self> where
Self: Unpin,
&'a mut self,
byte: u8,
buf: &'a mut Vec<u8>
) -> ReadUntilFuture<'a, Self> where
Self: Unpin,
fn read_line(&'a mut self, buf: &'a mut String) -> ReadLineFuture<'a, Self> where
Self: Unpin,
Self: Unpin,
fn lines(self) -> Lines<Self> where
Self: Unpin,
Self: Unpin,
fn split(self, byte: u8) -> Split<Self>
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 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]
impl<R> AsyncReadExt for R where
R: AsyncRead + ?Sized,
R: AsyncRead + ?Sized,
fn read(&'a mut self, buf: &'a mut [u8]) -> ReadFuture<'a, Self> where
Self: Unpin,
Self: Unpin,
fn read_vectored(
&'a mut self,
bufs: &'a mut [IoSliceMut<'a>]
) -> ReadVectoredFuture<'a, Self> where
Self: Unpin,
&'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>) -> ReadToEndFuture<'a, Self> where
Self: Unpin,
Self: Unpin,
fn read_to_string(
&'a mut self,
buf: &'a mut String
) -> ReadToStringFuture<'a, Self> where
Self: Unpin,
&'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,
Self: Unpin,
fn take(self, limit: u64) -> Take<Self>
fn bytes(self) -> Bytes<Self>
fn chain<R>(self, next: R) -> Chain<Self, R> where
R: AsyncRead,
R: AsyncRead,
fn boxed_reader<'a>(self) -> Pin<Box<dyn AsyncRead + 'a + Send>> where
Self: Send + 'a,
Self: Send + 'a,
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> BufReadExt for T where
T: AsyncBufRead + ?Sized,
[src]
T: AsyncBufRead + ?Sized,
fn read_until(
&'a mut self,
byte: u8,
buf: &'a mut Vec<u8>
) -> ReadUntilFuture<'a, Self> where
Self: Unpin,
[src]
&'a mut self,
byte: u8,
buf: &'a mut Vec<u8>
) -> ReadUntilFuture<'a, Self> where
Self: Unpin,
fn read_line(&'a mut self, buf: &'a mut String) -> ReadLineFuture<'a, Self> where
Self: Unpin,
[src]
Self: Unpin,
fn lines(self) -> Lines<Self> where
Self: Unpin,
[src]
Self: Unpin,
fn split(self, byte: u8) -> Split<Self>
[src]
impl<T> From<T> for T
[src]
impl<T> Instrument for T
[src]
fn instrument(self, span: Span) -> Instrumented<Self>
[src]
fn in_current_span(self) -> Instrumented<Self>
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T> ReadExt for T where
T: AsyncRead + ?Sized,
[src]
T: AsyncRead + ?Sized,
fn read(&'a mut self, buf: &'a mut [u8]) -> ReadFuture<'a, Self> where
Self: Unpin,
[src]
Self: Unpin,
fn read_vectored(
&'a mut self,
bufs: &'a mut [IoSliceMut<'a>]
) -> ReadVectoredFuture<'a, Self> where
Self: Unpin,
[src]
&'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>) -> ReadToEndFuture<'a, Self> where
Self: Unpin,
[src]
Self: Unpin,
fn read_to_string(
&'a mut self,
buf: &'a mut String
) -> ReadToStringFuture<'a, Self> where
Self: Unpin,
[src]
&'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,
[src]
Self: Unpin,
fn take(self, limit: u64) -> Take<Self>
[src]
fn by_ref(&mut self) -> &mut Self
[src]
fn bytes(self) -> Bytes<Self>
[src]
fn chain<R>(self, next: R) -> Chain<Self, R> where
R: AsyncRead,
[src]
R: AsyncRead,
impl<T> Same<T> for T
type Output = T
Should always be Self
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<V, T> VZip<V> for T where
V: MultiLane<T>,
V: MultiLane<T>,
fn vzip(self) -> V
impl<T> WithSubscriber for T
[src]
fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> where
S: Into<Dispatch>,
[src]
S: Into<Dispatch>,