pub struct Response { /* private fields */ }
Expand description
An HTTP response.
§Examples
use http_types::{Response, StatusCode};
let mut res = Response::new(StatusCode::Ok);
res.set_body("Hello, Nori!");
Implementations§
Source§impl Response
impl Response
Sourcepub fn status(&self) -> StatusCode
pub fn status(&self) -> StatusCode
Get the status
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 a mutable reference to a header.
Sourcepub fn header(&self, name: impl Into<HeaderName>) -> Option<&HeaderValues>
pub fn header(&self, name: impl Into<HeaderName>) -> Option<&HeaderValues>
Get an HTTP header.
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,
name: impl Into<HeaderName>,
values: impl ToHeaderValues,
) -> Option<HeaderValues>
pub fn insert_header( &mut self, name: impl Into<HeaderName>, values: impl ToHeaderValues, ) -> Option<HeaderValues>
Set an HTTP header.
§Examples
use http_types::{Method, Response, StatusCode, Url};
let mut req = Response::new(StatusCode::Ok);
req.insert_header("Content-Type", "text/plain");
Sourcepub fn append_header(
&mut self,
name: impl Into<HeaderName>,
values: impl ToHeaderValues,
)
pub fn append_header( &mut self, name: impl Into<HeaderName>, values: impl ToHeaderValues, )
Append a header to the headers.
Unlike insert
this function will not override the contents of a
header, but insert a header if there aren’t any. Or else append to
the existing list of headers.
§Examples
use http_types::{Response, StatusCode};
let mut res = Response::new(StatusCode::Ok);
res.append_header("Content-Type", "text/plain");
Sourcepub fn set_body(&mut self, body: impl Into<Body>)
pub fn set_body(&mut self, body: impl Into<Body>)
Set the body reader.
§Examples
use http_types::{Response, StatusCode};
let mut res = Response::new(StatusCode::Ok);
res.set_body("Hello, Nori!");
Sourcepub fn replace_body(&mut self, body: impl Into<Body>) -> Body
pub fn replace_body(&mut self, body: impl Into<Body>) -> Body
Replace the response body with a new body, returning the old body.
§Examples
use http_types::{Body, Method, Response, StatusCode, Url};
let mut req = Response::new(StatusCode::Ok);
req.set_body("Hello, Nori!");
let mut body: Body = req.replace_body("Hello, Chashu");
let mut string = String::new();
body.read_to_string(&mut string).await?;
assert_eq!(&string, "Hello, Nori!");
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.
§Examples
use http_types::{Body, Method, Response, StatusCode, Url};
let mut req = Response::new(StatusCode::Ok);
req.set_body("Hello, Nori!");
let mut body = "Hello, Chashu!".into();
req.swap_body(&mut body);
let mut string = String::new();
body.read_to_string(&mut string).await?;
assert_eq!(&string, "Hello, Nori!");
Sourcepub fn take_body(&mut self) -> Body
pub fn take_body(&mut self) -> Body
Take the response body, replacing it with an empty body.
§Examples
use http_types::{Body, Method, Response, StatusCode, Url};
let mut req = Response::new(StatusCode::Ok);
req.set_body("Hello, Nori!");
let mut body: Body = req.take_body();
let mut string = String::new();
body.read_to_string(&mut string).await?;
assert_eq!(&string, "Hello, Nori!");
Sourcepub async fn body_string(&mut self) -> Result<String>
pub async fn body_string(&mut self) -> Result<String>
Read the body as a string.
This consumes the response. If you want to read the body without
consuming the response, consider using the take_body
method and
then calling Body::into_string
or using the Response’s AsyncRead
implementation to read the body.
§Examples
use async_std::io::Cursor;
use http_types::{Body, Method, Response, StatusCode, Url};
let mut res = Response::new(StatusCode::Ok);
let cursor = Cursor::new("Hello Nori");
let body = Body::from_reader(cursor, None);
res.set_body(body);
assert_eq!(&res.body_string().await.unwrap(), "Hello Nori");
Sourcepub async fn body_bytes(&mut self) -> Result<Vec<u8>>
pub async fn body_bytes(&mut self) -> Result<Vec<u8>>
Read the body as bytes.
This consumes the Response
. If you want to read the body without
consuming the response, consider using the take_body
method and
then calling Body::into_bytes
or using the Response’s AsyncRead
implementation to read the body.
§Examples
use http_types::{Body, Method, Response, StatusCode, Url};
let bytes = vec![1, 2, 3];
let mut res = Response::new(StatusCode::Ok);
res.set_body(Body::from_bytes(bytes));
let bytes = res.body_bytes().await?;
assert_eq!(bytes, vec![1, 2, 3]);
Sourcepub async fn body_json<T: DeserializeOwned>(&mut self) -> Result<T>
pub async fn body_json<T: DeserializeOwned>(&mut self) -> Result<T>
Read the body as JSON.
This consumes the response. If you want to read the body without
consuming the response, consider using the take_body
method and
then calling Body::into_json
or using the Response’s AsyncRead
implementation to read the body.
§Examples
use http_types::convert::{Deserialize, Serialize};
use http_types::{Body, Method, Response, StatusCode, Url};
#[derive(Debug, Serialize, Deserialize)]
struct Cat {
name: String,
}
let cat = Cat {
name: String::from("chashu"),
};
let mut res = Response::new(StatusCode::Ok);
res.set_body(Body::from_json(&cat)?);
let cat: Cat = res.body_json().await?;
assert_eq!(&cat.name, "chashu");
Sourcepub async fn body_form<T: DeserializeOwned>(&mut self) -> Result<T>
pub async fn body_form<T: DeserializeOwned>(&mut self) -> Result<T>
Read the body as x-www-form-urlencoded
.
This consumes the request. If you want to read the body without
consuming the request, consider using the take_body
method and
then calling Body::into_json
or using the Response’s AsyncRead
implementation to read the body.
§Examples
use http_types::convert::{Deserialize, Serialize};
use http_types::{Body, Method, Response, StatusCode, Url};
#[derive(Debug, Serialize, Deserialize)]
struct Cat {
name: String,
}
let cat = Cat {
name: String::from("chashu"),
};
let mut res = Response::new(StatusCode::Ok);
res.set_body(Body::from_form(&cat)?);
let cat: Cat = res.body_form().await?;
assert_eq!(&cat.name, "chashu");
Sourcepub fn set_content_type(&mut self, mime: Mime) -> Option<HeaderValues>
pub fn set_content_type(&mut self, mime: Mime) -> Option<HeaderValues>
Set the response MIME.
Sourcepub fn content_type(&self) -> Option<Mime>
pub fn content_type(&self) -> Option<Mime>
Get the current content type
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 version(&self) -> Option<Version>
pub fn version(&self) -> Option<Version>
Get the HTTP version, if one has been set.
§Examples
use http_types::{Response, StatusCode, Version};
let mut res = Response::new(StatusCode::Ok);
assert_eq!(res.version(), None);
res.set_version(Some(Version::Http2_0));
assert_eq!(res.version(), Some(Version::Http2_0));
Sourcepub fn set_peer_addr(&mut self, peer_addr: Option<impl ToString>)
pub fn set_peer_addr(&mut self, peer_addr: Option<impl ToString>)
Sets a string representation of the peer address that this response was sent to. This might take the form of an ip/fqdn and port or a local socket address.
Sourcepub fn set_local_addr(&mut self, local_addr: Option<impl ToString>)
pub fn set_local_addr(&mut self, local_addr: Option<impl ToString>)
Sets a string representation of the local address that this response was sent on. This might take the form of an ip/fqdn and port, or a local socket address.
Sourcepub fn peer_addr(&self) -> Option<&str>
pub fn peer_addr(&self) -> Option<&str>
Get the peer socket address for the underlying transport, if appropriate
Sourcepub fn local_addr(&self) -> Option<&str>
pub fn local_addr(&self) -> Option<&str>
Get the local socket address for the underlying transport, if appropriate
Sourcepub fn set_version(&mut self, version: Option<Version>)
pub fn set_version(&mut self, version: Option<Version>)
Set the HTTP version.
§Examples
use http_types::{Response, StatusCode, Version};
let mut res = Response::new(StatusCode::Ok);
res.set_version(Some(Version::Http2_0));
Sourcepub fn set_status(&mut self, status: StatusCode)
pub fn set_status(&mut self, status: StatusCode)
Set the status.
Sourcepub fn send_trailers(&mut self) -> Sender
pub fn send_trailers(&mut self) -> Sender
Sends trailers to the a receiver.
Sourcepub fn recv_trailers(&mut self) -> Receiver ⓘ
pub fn recv_trailers(&mut self) -> Receiver ⓘ
Receive trailers from a sender.
Sourcepub fn has_trailers(&self) -> bool
pub fn has_trailers(&self) -> bool
Returns true
if sending trailers is in progress.
Sourcepub fn send_upgrade(&mut self) -> Sender
Available on unstable
only.
pub fn send_upgrade(&mut self) -> Sender
unstable
only.Sends an upgrade connection to the a receiver.
Sourcepub async fn recv_upgrade(&mut self) -> Receiver ⓘ
Available on unstable
only.
pub async fn recv_upgrade(&mut self) -> Receiver ⓘ
unstable
only.Receive an upgraded connection from a sender.
Sourcepub fn has_upgrade(&self) -> bool
Available on unstable
only.
pub fn has_upgrade(&self) -> bool
unstable
only.Returns true
if a protocol upgrade is in progress.
Sourcepub fn iter_mut(&mut self) -> IterMut<'_> ⓘ
pub fn iter_mut(&mut self) -> IterMut<'_> ⓘ
An iterator visiting all header pairs in arbitrary order, with mutable references to the values.
Sourcepub fn header_names(&self) -> Names<'_> ⓘ
pub fn header_names(&self) -> Names<'_> ⓘ
An iterator visiting all header names in arbitrary order.
Sourcepub fn header_values(&self) -> Values<'_> ⓘ
pub fn header_values(&self) -> Values<'_> ⓘ
An iterator visiting all header values in arbitrary order.
Sourcepub fn ext(&self) -> &Extensions
pub fn ext(&self) -> &Extensions
Returns a reference to the existing local.
Sourcepub fn ext_mut(&mut self) -> &mut Extensions
pub fn ext_mut(&mut self) -> &mut Extensions
Returns a mutuable reference to the existing local state.
§Examples
use http_types::{Response, StatusCode, Version};
let mut res = Response::new(StatusCode::Ok);
res.ext_mut().insert("hello from the extension");
assert_eq!(res.ext().get(), Some(&"hello from the extension"));
Trait Implementations§
Source§impl AsyncBufRead for Response
impl AsyncBufRead for Response
Source§impl AsyncRead for Response
impl AsyncRead for Response
Source§impl From<StatusCode> for Response
impl From<StatusCode> for Response
Source§fn from(s: StatusCode) -> Self
fn from(s: StatusCode) -> Self
Source§impl Index<&str> for Response
impl Index<&str> for Response
Source§fn 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
.
Source§type Output = HeaderValues
type Output = HeaderValues
Source§impl Index<HeaderName> for Response
impl Index<HeaderName> for Response
Source§fn 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
.
Source§type Output = HeaderValues
type Output = HeaderValues
Source§impl<'a> IntoIterator for &'a Response
impl<'a> IntoIterator for &'a Response
Source§impl<'a> IntoIterator for &'a mut Response
impl<'a> IntoIterator for &'a mut Response
Source§impl IntoIterator for Response
impl IntoIterator for Response
impl<'__pin> Unpin for Responsewhere
PinnedFieldsOf<__Origin<'__pin>>: Unpin,
Auto Trait Implementations§
impl Freeze for Response
impl !RefUnwindSafe for Response
impl Send for Response
impl Sync for Response
impl !UnwindSafe for Response
Blanket Implementations§
Source§impl<R> AsyncBufReadExt for Rwhere
R: AsyncBufRead + ?Sized,
impl<R> AsyncBufReadExt for Rwhere
R: AsyncBufRead + ?Sized,
Source§fn fill_buf(&mut self) -> FillBuf<'_, Self>where
Self: Unpin,
fn fill_buf(&mut self) -> FillBuf<'_, Self>where
Self: Unpin,
Source§fn read_until<'a>(
&'a mut self,
byte: u8,
buf: &'a mut Vec<u8>,
) -> ReadUntilFuture<'a, Self>where
Self: Unpin,
fn read_until<'a>(
&'a mut self,
byte: u8,
buf: &'a mut Vec<u8>,
) -> ReadUntilFuture<'a, Self>where
Self: Unpin,
Source§fn read_line<'a>(&'a mut self, buf: &'a mut String) -> ReadLineFuture<'a, Self>where
Self: Unpin,
fn read_line<'a>(&'a mut self, buf: &'a mut String) -> ReadLineFuture<'a, Self>where
Self: Unpin,
buf
until a newline (the 0xA byte) or EOF is found. Read moreSource§impl<R> AsyncBufReadExt for Rwhere
R: AsyncBufRead + ?Sized,
impl<R> AsyncBufReadExt for Rwhere
R: AsyncBufRead + ?Sized,
Source§fn fill_buf(&mut self) -> FillBuf<'_, Self>where
Self: Unpin,
fn fill_buf(&mut self) -> FillBuf<'_, Self>where
Self: Unpin,
Source§fn read_until<'a>(
&'a mut self,
byte: u8,
buf: &'a mut Vec<u8>,
) -> ReadUntilFuture<'a, Self>where
Self: Unpin,
fn read_until<'a>(
&'a mut self,
byte: u8,
buf: &'a mut Vec<u8>,
) -> ReadUntilFuture<'a, Self>where
Self: Unpin,
Source§fn read_line<'a>(&'a mut self, buf: &'a mut String) -> ReadLineFuture<'a, Self>where
Self: Unpin,
fn read_line<'a>(&'a mut self, buf: &'a mut String) -> ReadLineFuture<'a, Self>where
Self: Unpin,
buf
until a newline (the 0xA byte) or EOF is found. Read moreSource§impl<R> AsyncReadExt for R
impl<R> AsyncReadExt for R
Source§fn read<'a>(&'a mut self, buf: &'a mut [u8]) -> ReadFuture<'a, Self>where
Self: Unpin,
fn read<'a>(&'a mut self, buf: &'a mut [u8]) -> ReadFuture<'a, Self>where
Self: Unpin,
Source§fn read_vectored<'a>(
&'a mut self,
bufs: &'a mut [IoSliceMut<'a>],
) -> ReadVectoredFuture<'a, Self>where
Self: Unpin,
fn read_vectored<'a>(
&'a mut self,
bufs: &'a mut [IoSliceMut<'a>],
) -> ReadVectoredFuture<'a, Self>where
Self: Unpin,
Source§fn read_to_end<'a>(
&'a mut self,
buf: &'a mut Vec<u8>,
) -> ReadToEndFuture<'a, Self>where
Self: Unpin,
fn read_to_end<'a>(
&'a mut self,
buf: &'a mut Vec<u8>,
) -> ReadToEndFuture<'a, Self>where
Self: Unpin,
Source§fn read_to_string<'a>(
&'a mut self,
buf: &'a mut String,
) -> ReadToStringFuture<'a, Self>where
Self: Unpin,
fn read_to_string<'a>(
&'a mut self,
buf: &'a mut String,
) -> ReadToStringFuture<'a, Self>where
Self: Unpin,
Source§fn read_exact<'a>(&'a mut self, buf: &'a mut [u8]) -> ReadExactFuture<'a, Self>where
Self: Unpin,
fn read_exact<'a>(&'a mut self, buf: &'a mut [u8]) -> ReadExactFuture<'a, Self>where
Self: Unpin,
buf
. Read moreSource§fn take(self, limit: u64) -> Take<Self>where
Self: Sized,
fn take(self, limit: u64) -> Take<Self>where
Self: Sized,
limit
bytes from it. Read moreSource§impl<R> AsyncReadExt for R
impl<R> AsyncReadExt for R
Source§fn read<'a>(&'a mut self, buf: &'a mut [u8]) -> ReadFuture<'a, Self>where
Self: Unpin,
fn read<'a>(&'a mut self, buf: &'a mut [u8]) -> ReadFuture<'a, Self>where
Self: Unpin,
Source§fn read_vectored<'a>(
&'a mut self,
bufs: &'a mut [IoSliceMut<'a>],
) -> ReadVectoredFuture<'a, Self>where
Self: Unpin,
fn read_vectored<'a>(
&'a mut self,
bufs: &'a mut [IoSliceMut<'a>],
) -> ReadVectoredFuture<'a, Self>where
Self: Unpin,
Source§fn read_to_end<'a>(
&'a mut self,
buf: &'a mut Vec<u8>,
) -> ReadToEndFuture<'a, Self>where
Self: Unpin,
fn read_to_end<'a>(
&'a mut self,
buf: &'a mut Vec<u8>,
) -> ReadToEndFuture<'a, Self>where
Self: Unpin,
Source§fn read_to_string<'a>(
&'a mut self,
buf: &'a mut String,
) -> ReadToStringFuture<'a, Self>where
Self: Unpin,
fn read_to_string<'a>(
&'a mut self,
buf: &'a mut String,
) -> ReadToStringFuture<'a, Self>where
Self: Unpin,
Source§fn read_exact<'a>(&'a mut self, buf: &'a mut [u8]) -> ReadExactFuture<'a, Self>where
Self: Unpin,
fn read_exact<'a>(&'a mut self, buf: &'a mut [u8]) -> ReadExactFuture<'a, Self>where
Self: Unpin,
buf
. Read moreSource§fn take(self, limit: u64) -> Take<Self>where
Self: Sized,
fn take(self, limit: u64) -> Take<Self>where
Self: Sized,
limit
bytes from it. Read moreSource§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> BufReadExt for Twhere
T: AsyncBufRead + ?Sized,
impl<T> BufReadExt for Twhere
T: AsyncBufRead + ?Sized,
Source§fn read_until<'a>(
&'a mut self,
byte: u8,
buf: &'a mut Vec<u8>,
) -> ReadUntilFuture<'a, Self>where
Self: Unpin,
fn read_until<'a>(
&'a mut self,
byte: u8,
buf: &'a mut Vec<u8>,
) -> ReadUntilFuture<'a, Self>where
Self: Unpin,
Source§fn read_line<'a>(&'a mut self, buf: &'a mut String) -> ReadLineFuture<'a, Self>where
Self: Unpin,
fn read_line<'a>(&'a mut self, buf: &'a mut String) -> ReadLineFuture<'a, Self>where
Self: Unpin,
buf
until a newline (the 0xA byte) is
reached. Read moreSource§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> ReadExt for T
impl<T> ReadExt for T
Source§fn read<'a>(&'a mut self, buf: &'a mut [u8]) -> ReadFuture<'a, Self>where
Self: Unpin,
fn read<'a>(&'a mut self, buf: &'a mut [u8]) -> ReadFuture<'a, Self>where
Self: Unpin,
Source§fn read_vectored<'a>(
&'a mut self,
bufs: &'a mut [IoSliceMut<'a>],
) -> ReadVectoredFuture<'a, Self>where
Self: Unpin,
fn read_vectored<'a>(
&'a mut self,
bufs: &'a mut [IoSliceMut<'a>],
) -> ReadVectoredFuture<'a, Self>where
Self: Unpin,
Source§fn read_to_end<'a>(
&'a mut self,
buf: &'a mut Vec<u8>,
) -> ReadToEndFuture<'a, Self>where
Self: Unpin,
fn read_to_end<'a>(
&'a mut self,
buf: &'a mut Vec<u8>,
) -> ReadToEndFuture<'a, Self>where
Self: Unpin,
Source§fn read_to_string<'a>(
&'a mut self,
buf: &'a mut String,
) -> ReadToStringFuture<'a, Self>where
Self: Unpin,
fn read_to_string<'a>(
&'a mut self,
buf: &'a mut String,
) -> ReadToStringFuture<'a, Self>where
Self: Unpin,
Source§fn read_exact<'a>(&'a mut self, buf: &'a mut [u8]) -> ReadExactFuture<'a, Self>where
Self: Unpin,
fn read_exact<'a>(&'a mut self, buf: &'a mut [u8]) -> ReadExactFuture<'a, Self>where
Self: Unpin,
buf
. Read moreSource§fn take(self, limit: u64) -> Take<Self>where
Self: Sized,
fn take(self, limit: u64) -> Take<Self>where
Self: Sized,
limit
bytes from it. Read moreSource§fn by_ref(&mut self) -> &mut Selfwhere
Self: Sized,
fn by_ref(&mut self) -> &mut Selfwhere
Self: Sized,
Read
. Read more