pub struct Response { /* private fields */ }
Expand description
HTTP response builder with a fluent API for creating responses.
The Response
struct provides a convenient, chainable interface for building
HTTP responses. It supports setting status codes, headers, and body content
with type-safe methods and automatic content-type detection.
§Examples
§Basic Responses
use torch_web::Response;
// Simple text response
let response = Response::ok().body("Hello, World!");
// JSON response
let data = serde_json::json!({"message": "Hello", "status": "success"});
let response = Response::ok().json(&data);
// Custom status code
let response = Response::with_status(StatusCode::CREATED)
.body("Resource created");
§With Headers
use torch_web::Response;
let response = Response::ok()
.header("Content-Type", "application/json")
.header("X-API-Version", "1.0")
.header("Cache-Control", "no-cache")
.body(r#"{"data": "value"}"#);
§Error Responses
use torch_web::Response;
// 404 Not Found
let response = Response::not_found();
// 400 Bad Request with custom message
let response = Response::bad_request()
.body("Invalid request parameters");
// 500 Internal Server Error
let response = Response::internal_error()
.body("Something went wrong");
§Redirects
use torch_web::Response;
// Temporary redirect
let response = Response::redirect_temporary("/new-location");
// Permanent redirect
let response = Response::redirect_permanent("/moved-permanently");
§File Downloads
use torch_web::Response;
let file_data = std::fs::read("document.pdf")?;
let response = Response::ok()
.header("Content-Type", "application/pdf")
.header("Content-Disposition", "attachment; filename=\"document.pdf\"")
.body(file_data);
Implementations§
Source§impl Response
impl Response
Sourcepub fn with_status(status: StatusCode) -> Self
pub fn with_status(status: StatusCode) -> Self
Create a response with a specific status code
Sourcepub fn internal_error() -> Self
pub fn internal_error() -> Self
Create a 500 Internal Server Error response
Sourcepub fn bad_request() -> Self
pub fn bad_request() -> Self
Create a 400 Bad Request response
Sourcepub fn no_content() -> Self
pub fn no_content() -> Self
Create a 204 No Content response
Create a 401 Unauthorized response
Sourcepub fn unprocessable_entity() -> Self
pub fn unprocessable_entity() -> Self
Create a 422 Unprocessable Entity response
Sourcepub fn too_many_requests() -> Self
pub fn too_many_requests() -> Self
Create a 429 Too Many Requests response
Sourcepub fn status(self, status: StatusCode) -> Self
pub fn status(self, status: StatusCode) -> Self
Set the status code
Sourcepub fn body_from_bytes(self, body: Vec<u8>) -> Self
pub fn body_from_bytes(self, body: Vec<u8>) -> Self
Set the response body from bytes
Sourcepub fn content_type(self, content_type: &str) -> Self
pub fn content_type(self, content_type: &str) -> Self
Set the Content-Type header
Sourcepub fn json<T: Serialize>(self, value: &T) -> Result<Self, Error>
pub fn json<T: Serialize>(self, value: &T) -> Result<Self, Error>
Set response as JSON and serialize the value (requires “json” feature)
Sourcepub fn redirect(status: StatusCode, location: &str) -> Self
pub fn redirect(status: StatusCode, location: &str) -> Self
Redirect to another URL
Sourcepub fn redirect_found(location: &str) -> Self
pub fn redirect_found(location: &str) -> Self
Redirect with 302 Found status
Sourcepub fn redirect_permanent(location: &str) -> Self
pub fn redirect_permanent(location: &str) -> Self
Redirect with 301 Moved Permanently status
Sourcepub fn status_code(&self) -> StatusCode
pub fn status_code(&self) -> StatusCode
Get the status code
Sourcepub fn status_code_mut(&mut self) -> &mut StatusCode
pub fn status_code_mut(&mut self) -> &mut StatusCode
Get a mutable reference to the status code
Sourcepub fn body_bytes(&self) -> &[u8] ⓘ
pub fn body_bytes(&self) -> &[u8] ⓘ
Get the body as bytes (alias for body_data)
Sourcepub fn into_hyper_response(self) -> Response<Full<Bytes>>
pub fn into_hyper_response(self) -> Response<Full<Bytes>>
Convert to hyper Response