Struct Response
pub struct Response<T> { /* private fields */ }
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 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<()>> {
// ...