pub struct HttpResponse {
pub status: u16,
pub headers: LazyHeaders,
pub body: Vec<u8>,
/* private fields */
}Expand description
HTTP response wrapper
The body is stored as Vec<u8> for backwards compatibility.
For zero-copy body handling, use with_bytes_body() or body_bytes().
§Performance Note
Response creation is optimized for minimal allocation:
headersusesLazyHeaderswhich doesn’t allocate until first insertbodyusesOption<Vec<u8>>which doesn’t allocate for empty responses- Use
FastResponsefromarmature_core::fast_responsefor even faster creation
Fields§
§status: u16§headers: LazyHeadersResponse headers with lazy allocation.
body: Vec<u8>Response body as raw bytes (legacy field for compatibility).
Implementations§
Source§impl HttpResponse
impl HttpResponse
Sourcepub fn new(status: u16) -> Self
pub fn new(status: u16) -> Self
Create a new response with the given status code.
This is optimized for minimal allocation - headers use LazyHeaders
which doesn’t allocate until first insert, and body uses Vec::new()
which is zero-cost.
Sourcepub fn with_capacity(status: u16, capacity: usize) -> Self
pub fn with_capacity(status: u16, capacity: usize) -> Self
Sourcepub fn ok_preallocated() -> Self
pub fn ok_preallocated() -> Self
Create a 200 OK response with pre-allocated buffer (512 bytes default).
Sourcepub fn no_content() -> Self
pub fn no_content() -> Self
Create a 204 No Content response.
Sourcepub fn bad_request() -> Self
pub fn bad_request() -> Self
Create a 400 Bad Request response.
Sourcepub fn internal_server_error() -> Self
pub fn internal_server_error() -> Self
Create a 500 Internal Server Error response.
pub fn with_body(self, body: Vec<u8>) -> Self
Sourcepub fn with_bytes_body(self, bytes: Bytes) -> Self
pub fn with_bytes_body(self, bytes: Bytes) -> Self
Set the body using Bytes (zero-copy).
This is the most efficient way to set response body data, as it can be passed directly to Hyper without copying.
Sourcepub fn with_static_body(self, body: &'static [u8]) -> Self
pub fn with_static_body(self, body: &'static [u8]) -> Self
Set the body from a static byte slice (zero-copy).
Sourcepub fn body_bytes(&self) -> Bytes
pub fn body_bytes(&self) -> Bytes
Get the body as Bytes (zero-copy if stored as Bytes).
This is the key method for zero-copy Hyper body passthrough.
If body was set via with_bytes_body(), returns the Bytes directly (O(1)).
Otherwise, converts from Vec
Sourcepub fn into_body_bytes(self) -> Bytes
pub fn into_body_bytes(self) -> Bytes
Consume the response and return body as Bytes (zero-copy).
This is the most efficient way to get the body for Hyper, as it avoids cloning when body_bytes is set.
Sourcepub fn has_bytes_body(&self) -> bool
pub fn has_bytes_body(&self) -> bool
Check if using zero-copy Bytes storage.
pub fn with_header(self, key: String, value: String) -> Self
Sourcepub fn with_headers(self, headers: HashMap<String, String>) -> Self
pub fn with_headers(self, headers: HashMap<String, String>) -> Self
Set multiple headers from a HashMap.
Sourcepub fn with_status_and_headers(
status: u16,
headers: HashMap<String, String>,
) -> Self
pub fn with_status_and_headers( status: u16, headers: HashMap<String, String>, ) -> Self
Create a response with status and headers (for CORS preflight, etc.).
Sourcepub fn from_parts(
status: u16,
headers: HashMap<String, String>,
body: Vec<u8>,
) -> Self
pub fn from_parts( status: u16, headers: HashMap<String, String>, body: Vec<u8>, ) -> Self
Create a response with all components (for compatibility).
This is useful when you need to construct a response with all parts at once.
Sourcepub fn accepted() -> Self
pub fn accepted() -> Self
Create an accepted response (202).
§Example
use armature_core::HttpResponse;
let response = HttpResponse::accepted();
assert_eq!(response.status, 202);Create an unauthorized response (401).
§Example
use armature_core::HttpResponse;
let response = HttpResponse::unauthorized();
assert_eq!(response.status, 401);Sourcepub fn forbidden() -> Self
pub fn forbidden() -> Self
Create a forbidden response (403).
§Example
use armature_core::HttpResponse;
let response = HttpResponse::forbidden();
assert_eq!(response.status, 403);Sourcepub fn conflict() -> Self
pub fn conflict() -> Self
Create a conflict response (409).
§Example
use armature_core::HttpResponse;
let response = HttpResponse::conflict();
assert_eq!(response.status, 409);Create a service unavailable response (503).
§Example
use armature_core::HttpResponse;
let response = HttpResponse::service_unavailable();
assert_eq!(response.status, 503);Sourcepub fn json<T: Serialize>(value: &T) -> Result<Self, Error>
pub fn json<T: Serialize>(value: &T) -> Result<Self, Error>
Shorthand for creating a JSON response with 200 OK status.
§Example
use armature_core::HttpResponse;
use serde_json::json;
let response = HttpResponse::json(&json!({"message": "Hello"})).unwrap();
assert_eq!(response.status, 200);Sourcepub fn html(content: impl Into<String>) -> Self
pub fn html(content: impl Into<String>) -> Self
Create an HTML response with 200 OK status.
§Example
use armature_core::HttpResponse;
let response = HttpResponse::html("<h1>Hello</h1>");
assert_eq!(response.status, 200);
assert_eq!(response.headers.get("Content-Type"), Some(&"text/html; charset=utf-8".to_string()));Sourcepub fn text(content: impl Into<String>) -> Self
pub fn text(content: impl Into<String>) -> Self
Create a plain text response with 200 OK status.
§Example
use armature_core::HttpResponse;
let response = HttpResponse::text("Hello, World!");
assert_eq!(response.status, 200);
assert_eq!(response.headers.get("Content-Type"), Some(&"text/plain; charset=utf-8".to_string()));Sourcepub fn redirect(url: impl Into<String>) -> Self
pub fn redirect(url: impl Into<String>) -> Self
Create a redirect response (302 Found).
§Example
use armature_core::HttpResponse;
let response = HttpResponse::redirect("https://example.com");
assert_eq!(response.status, 302);
assert_eq!(response.headers.get("Location"), Some(&"https://example.com".to_string()));Sourcepub fn redirect_permanent(url: impl Into<String>) -> Self
pub fn redirect_permanent(url: impl Into<String>) -> Self
Create a permanent redirect response (301 Moved Permanently).
§Example
use armature_core::HttpResponse;
let response = HttpResponse::redirect_permanent("https://example.com");
assert_eq!(response.status, 301);Sourcepub fn see_other(url: impl Into<String>) -> Self
pub fn see_other(url: impl Into<String>) -> Self
Create a see other redirect response (303 See Other). Useful after a POST request to redirect to a GET.
§Example
use armature_core::HttpResponse;
let response = HttpResponse::see_other("/success");
assert_eq!(response.status, 303);Sourcepub fn empty() -> Self
pub fn empty() -> Self
Alias for no_content() - returns 204 with empty body.
§Example
use armature_core::HttpResponse;
let response = HttpResponse::empty();
assert_eq!(response.status, 204);Sourcepub fn content_type(self, content_type: impl Into<String>) -> Self
pub fn content_type(self, content_type: impl Into<String>) -> Self
Set the Content-Type header.
§Example
use armature_core::HttpResponse;
let response = HttpResponse::ok().content_type("application/xml");
assert_eq!(response.headers.get("Content-Type"), Some(&"application/xml".to_string()));Sourcepub fn cache_control(self, directive: impl Into<String>) -> Self
pub fn cache_control(self, directive: impl Into<String>) -> Self
Set the Cache-Control header.
§Example
use armature_core::HttpResponse;
let response = HttpResponse::ok().cache_control("max-age=3600");Sourcepub fn no_cache(self) -> Self
pub fn no_cache(self) -> Self
Mark the response as not cacheable.
§Example
use armature_core::HttpResponse;
let response = HttpResponse::ok().no_cache();Set a cookie on the response.
§Example
use armature_core::HttpResponse;
let response = HttpResponse::ok().cookie("session", "abc123; HttpOnly; Secure");Sourcepub fn body_string(&self) -> String
pub fn body_string(&self) -> String
Get the response body as a string (lossy UTF-8 conversion).
Sourcepub fn is_success(&self) -> bool
pub fn is_success(&self) -> bool
Check if the response is successful (2xx status code).
Sourcepub fn is_redirect(&self) -> bool
pub fn is_redirect(&self) -> bool
Check if the response is a redirect (3xx status code).
Sourcepub fn is_client_error(&self) -> bool
pub fn is_client_error(&self) -> bool
Check if the response is a client error (4xx status code).
Sourcepub fn is_server_error(&self) -> bool
pub fn is_server_error(&self) -> bool
Check if the response is a server error (5xx status code).
Source§impl HttpResponse
impl HttpResponse
Sourcepub fn into_chunks(self) -> ResponseChunks
pub fn into_chunks(self) -> ResponseChunks
Convert to vectored response chunks.
Use this for efficient vectored writes when you have direct socket access.
Sourcepub fn to_vectored(&self) -> ResponseChunks
pub fn to_vectored(&self) -> ResponseChunks
Get IoSlices for vectored write.
This is a convenience method that creates ResponseChunks
and returns the slices. For repeated use, prefer into_chunks().