pub struct Response {
pub status: u16,
pub body: Body,
pub content_type: &'static str,
pub headers: Headers,
}Expand description
An HTTP response to be sent to the client.
Construct responses using the factory methods (Response::text,
Response::json, Response::file, etc.) and customise with
Response::with_header and status code assignment.
§Examples
// Plain text
Response::text("Hello, world!")
// JSON (Schema-JIT serialization)
Response::json(&user)
// Custom status + headers
let mut res = Response::json(&item);
res.status = 201;
res.with_header("Location", "/items/42")Fields§
§status: u16§body: Body§content_type: &'static str§headers: HeadersCustom response headers — stored inline (stack) for ≤8 headers, falling back to heap for more. No allocation for common cases.
Implementations§
Source§impl Response
impl Response
Sourcepub fn with_header(
self,
name: &'static str,
value: impl IntoHeaderValue,
) -> Self
pub fn with_header( self, name: &'static str, value: impl IntoHeaderValue, ) -> Self
Builder-style method to append an HTTP response header.
The value may be a &'static str, String, or any integer type.
Short values (≤ 64 bytes) are stored inline on the stack; longer
values fall back to heap allocation.
Sourcepub fn text_static(body: &'static [u8]) -> Self
pub fn text_static(body: &'static [u8]) -> Self
200 OK with a zero-copy static plain-text body. Avoids heap allocation — ideal for fixed responses like TFB plaintext.
Sourcepub fn json_bytes(body: impl Into<Vec<u8>>) -> Self
pub fn json_bytes(body: impl Into<Vec<u8>>) -> Self
200 OK with a pre-serialized JSON byte body.
Use Response::json() when you have a typed value to serialize.
Sourcepub fn json<T: Serialize>(val: &T) -> Self
pub fn json<T: Serialize>(val: &T) -> Self
200 OK — serializes a typed value to JSON using the Schema-JIT engine. This is the primary way to return structured data from a handler.
Sourcepub fn json_static(body: &'static [u8]) -> Self
pub fn json_static(body: &'static [u8]) -> Self
Sourcepub fn raw(bytes: &'static [u8]) -> Self
pub fn raw(bytes: &'static [u8]) -> Self
Emit a fully pre-baked HTTP/1.1 response verbatim.
The supplied bytes must be a complete, valid HTTP/1.1 response
(status line + headers + blank line + body). The worker writes them
as-is, bypassing every header serialization step.
This is the absolute fastest response path — a single memcpy
into the connection’s write_buf, then one write(2) syscall.
§Safety contract
You must include Date:, Content-Length:, and Content-Type: headers
yourself. Chopin will NOT add them for Body::Raw responses.
§Example
// Build once at program start:
static PONG: &[u8] = b"HTTP/1.1 200 OK\r\n\
Server: chopin\r\n\
Content-Type: text/plain\r\n\
Content-Length: 4\r\n\
Connection: keep-alive\r\n\
\r\n\
pong";
fn pong(_ctx: Context) -> Response { Response::raw(PONG) }Sourcepub fn server_error() -> Self
pub fn server_error() -> Self
500 Internal Server Error.
Sourcepub fn bad_request() -> Self
pub fn bad_request() -> Self
400 Bad Request.
401 Unauthorized.
Sourcepub fn stream(iter: impl Iterator<Item = Vec<u8>> + Send + 'static) -> Self
pub fn stream(iter: impl Iterator<Item = Vec<u8>> + Send + 'static) -> Self
Chunked streaming response with application/octet-stream content type.