pub struct Response {
pub status: StatusCode,
pub version: Version,
pub headers: HeaderMap,
pub extensions: Extensions,
pub body: Body,
}
Expand description
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 axol_http::{Request, Response, StatusCode};
fn respond_to(req: Request<()>) -> axol_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 axol_http::{Request, Response, StatusCode};
fn not_found(_req: Request<()>) -> axol_http::Result<Response<()>> {
Response::builder()
.status(StatusCode::NOT_FOUND)
.body(())
}
Or otherwise inspecting the result of a request:
use axol_http::{Request, Response};
fn get(url: &str) -> axol_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 axol_http::Response;
use serde::de;
fn deserialize<T>(res: Response<Vec<u8>>) -> serde_json::Result<Response<T>>
where for<'de> T: de::Deserialize<'de>,
{
let (parts, body) = res.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 axol_http::Response;
use serde::ser;
fn serialize<T>(res: Response<T>) -> serde_json::Result<Response<Vec<u8>>>
where T: ser::Serialize,
{
let (parts, body) = res.into_parts();
let body = serde_json::to_vec(&body)?;
Ok(Response::from_parts(parts, body))
}
Fields§
§status: StatusCode
The response’s status
version: Version
The response’s version
headers: HeaderMap
The response’s headers
extensions: Extensions
The response’s extensions
body: Body
The response’s body
Implementations§
Source§impl Response
impl Response
Sourcepub fn builder() -> Builder
pub fn builder() -> Builder
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();
Sourcepub fn new(body: impl Into<Body>) -> Response
pub fn new(body: impl Into<Body>) -> Response
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");
Sourcepub fn from_parts(parts: ResponseParts, body: impl Into<Body>) -> Response
pub fn from_parts(parts: ResponseParts, body: impl Into<Body>) -> Response
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");
Sourcepub fn into_parts(self) -> (ResponseParts, Body)
pub fn into_parts(self) -> (ResponseParts, Body)
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);
Sourcepub fn with_status(self, status: StatusCode) -> Self
pub fn with_status(self, status: StatusCode) -> Self
Sets status code of response