Struct lamedh_http::Response [−][src]
Represents an HTTP response
An HTTP response consists of a head and a potentially optional body. The body
component is generic, enabling arbitrary types to represent the HTTP body.
For example, the body could be Vec<u8>
, a Stream
of byte chunks, or a
value that has been deserialized.
Typically you’ll work with responses on the client side as the result of
sending a Request
and on the server you’ll be generating a Response
to
send back to the client.
Examples
Creating a Response
to return
use http::{Request, Response, StatusCode}; fn respond_to(req: Request<()>) -> http::Result<Response<()>> { let mut builder = Response::builder() .header("Foo", "Bar") .status(StatusCode::OK); if req.headers().contains_key("Another-Header") { builder = builder.header("Another-Header", "Ack"); } builder.body(()) }
A simple 404 handler
use http::{Request, Response, StatusCode}; fn not_found(_req: Request<()>) -> http::Result<Response<()>> { Response::builder() .status(StatusCode::NOT_FOUND) .body(()) }
Or otherwise inspecting the result of a request:
use http::{Request, Response}; fn get(url: &str) -> http::Result<Response<()>> { // ... } let response = get("https://www.rust-lang.org/").unwrap(); if !response.status().is_success() { panic!("failed to get a successful response status!"); } if let Some(date) = response.headers().get("Date") { // we've got a `Date` header! } let body = response.body(); // ...
Deserialize a response of bytes via json:
use http::Response; use serde::de; fn deserialize<T>(req: Response<Vec<u8>>) -> serde_json::Result<Response<T>> where for<'de> T: de::Deserialize<'de>, { let (parts, body) = req.into_parts(); let body = serde_json::from_slice(&body)?; Ok(Response::from_parts(parts, body)) }
Or alternatively, serialize the body of a response to json
use http::Response; use serde::ser; fn serialize<T>(req: Response<T>) -> serde_json::Result<Response<Vec<u8>>> where T: ser::Serialize, { let (parts, body) = req.into_parts(); let body = serde_json::to_vec(&body)?; Ok(Response::from_parts(parts, body)) }
Implementations
impl Response<()>
[src]
pub fn builder() -> Builder
[src]
Creates a new builder-style object to manufacture a Response
This method returns an instance of Builder
which can be used to
create a Response
.
Examples
let response = Response::builder() .status(200) .header("X-Custom-Foo", "Bar") .body(()) .unwrap();
impl<T> Response<T>
[src]
pub fn new(body: T) -> Response<T>
[src]
Creates a new blank Response
with the body
The component ports of this response will be set to their default, e.g. the ok status, no headers, etc.
Examples
let response = Response::new("hello world"); assert_eq!(response.status(), StatusCode::OK); assert_eq!(*response.body(), "hello world");
pub fn from_parts(parts: Parts, body: T) -> Response<T>
[src]
Creates a new Response
with the given head and body
Examples
let response = Response::new("hello world"); let (mut parts, body) = response.into_parts(); parts.status = StatusCode::BAD_REQUEST; let response = Response::from_parts(parts, body); assert_eq!(response.status(), StatusCode::BAD_REQUEST); assert_eq!(*response.body(), "hello world");
pub fn status(&self) -> StatusCode
[src]
Returns the StatusCode
.
Examples
let response: Response<()> = Response::default(); assert_eq!(response.status(), StatusCode::OK);
pub fn status_mut(&mut self) -> &mut StatusCode
[src]
Returns a mutable reference to the associated StatusCode
.
Examples
let mut response: Response<()> = Response::default(); *response.status_mut() = StatusCode::CREATED; assert_eq!(response.status(), StatusCode::CREATED);
pub fn version(&self) -> Version
[src]
Returns a reference to the associated version.
Examples
let response: Response<()> = Response::default(); assert_eq!(response.version(), Version::HTTP_11);
pub fn version_mut(&mut self) -> &mut Version
[src]
Returns a mutable reference to the associated version.
Examples
let mut response: Response<()> = Response::default(); *response.version_mut() = Version::HTTP_2; assert_eq!(response.version(), Version::HTTP_2);
pub fn headers(&self) -> &HeaderMap<HeaderValue>
[src]
Returns a reference to the associated header field map.
Examples
let response: Response<()> = Response::default(); assert!(response.headers().is_empty());
pub fn headers_mut(&mut self) -> &mut HeaderMap<HeaderValue>
[src]
Returns a mutable reference to the associated header field map.
Examples
let mut response: Response<()> = Response::default(); response.headers_mut().insert(HOST, HeaderValue::from_static("world")); assert!(!response.headers().is_empty());
pub fn extensions(&self) -> &Extensions
[src]
Returns a reference to the associated extensions.
Examples
let response: Response<()> = Response::default(); assert!(response.extensions().get::<i32>().is_none());
pub fn extensions_mut(&mut self) -> &mut Extensions
[src]
Returns a mutable reference to the associated extensions.
Examples
let mut response: Response<()> = Response::default(); response.extensions_mut().insert("hello"); assert_eq!(response.extensions().get(), Some(&"hello"));
pub fn body(&self) -> &T
[src]
Returns a reference to the associated HTTP body.
Examples
let response: Response<String> = Response::default(); assert!(response.body().is_empty());
pub fn body_mut(&mut self) -> &mut T
[src]
Returns a mutable reference to the associated HTTP body.
Examples
let mut response: Response<String> = Response::default(); response.body_mut().push_str("hello world"); assert!(!response.body().is_empty());
pub fn into_body(self) -> T
[src]
Consumes the response, returning just the body.
Examples
let response = Response::new(10); let body = response.into_body(); assert_eq!(body, 10);
pub fn into_parts(self) -> (Parts, T)
[src]
Consumes the response returning the head and body parts.
Examples
let response: Response<()> = Response::default(); let (parts, body) = response.into_parts(); assert_eq!(parts.status, StatusCode::OK);
pub fn map<F, U>(self, f: F) -> Response<U> where
F: FnOnce(T) -> U,
[src]
F: FnOnce(T) -> U,
Consumes the response returning a new response with body mapped to the return type of the passed in function.
Examples
let response = Response::builder().body("some string").unwrap(); let mapped_response: Response<&[u8]> = response.map(|b| { assert_eq!(b, "some string"); b.as_bytes() }); assert_eq!(mapped_response.body(), &"some string".as_bytes());
Trait Implementations
impl<B> Body for Response<B> where
B: Body,
[src]
B: Body,
type Data = <B as Body>::Data
Values yielded by the Body
.
type Error = <B as Body>::Error
The error type this Body
might generate.
pub fn poll_data(
self: Pin<&mut Response<B>>,
cx: &mut Context<'_>
) -> Poll<Option<Result<<Response<B> as Body>::Data, <Response<B> as Body>::Error>>>
[src]
self: Pin<&mut Response<B>>,
cx: &mut Context<'_>
) -> Poll<Option<Result<<Response<B> as Body>::Data, <Response<B> as Body>::Error>>>
pub fn poll_trailers(
self: Pin<&mut Response<B>>,
cx: &mut Context<'_>
) -> Poll<Result<Option<HeaderMap<HeaderValue>>, <Response<B> as Body>::Error>>
[src]
self: Pin<&mut Response<B>>,
cx: &mut Context<'_>
) -> Poll<Result<Option<HeaderMap<HeaderValue>>, <Response<B> as Body>::Error>>
pub fn is_end_stream(&self) -> bool
[src]
pub fn size_hint(&self) -> SizeHint
[src]
pub fn data(&mut self) -> Data<'_, Self> where
Self: Unpin,
[src]
Self: Unpin,
pub fn trailers(&mut self) -> Trailers<'_, Self> where
Self: Unpin,
[src]
Self: Unpin,
pub fn map_data<F, B>(self, f: F) -> MapData<Self, F> where
F: FnMut(Self::Data) -> B,
B: Buf,
[src]
F: FnMut(Self::Data) -> B,
B: Buf,
pub fn map_err<F, E>(self, f: F) -> MapErr<Self, F> where
F: FnMut(Self::Error) -> E,
[src]
F: FnMut(Self::Error) -> E,
pub fn boxed(self) -> BoxBody<Self::Data, Self::Error> where
Self: Send + Sync + 'static,
[src]
Self: Send + Sync + 'static,
impl<T> Debug for Response<T> where
T: Debug,
[src]
T: Debug,
impl<T> Default for Response<T> where
T: Default,
[src]
T: Default,
impl<B> IntoResponse for Response<B> where
B: Into<Body>,
[src]
B: Into<Body>,
fn into_response(self) -> Response<Body>
[src]
Auto Trait Implementations
impl<T> !RefUnwindSafe for Response<T>
impl<T> Send for Response<T> where
T: Send,
T: Send,
impl<T> Sync for Response<T> where
T: Sync,
T: Sync,
impl<T> Unpin for Response<T> where
T: Unpin,
T: Unpin,
impl<T> !UnwindSafe for Response<T>
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
pub fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> From<T> for T
[src]
impl<T> Instrument for T
[src]
pub fn instrument(self, span: Span) -> Instrumented<Self>
[src]
pub 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, 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.
pub 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>,