pub struct Response { /* private fields */ }Expand description
HTTP response.
Implementations§
Source§impl Response
impl Response
Sourcepub fn with_status(status: StatusCode) -> Self
pub fn with_status(status: StatusCode) -> Self
Create a response with the given status.
Sourcepub fn no_content() -> Self
pub fn no_content() -> Self
Create a 204 No Content response.
Sourcepub fn internal_error() -> Self
pub fn internal_error() -> Self
Create a 500 Internal Server Error response.
Sourcepub fn partial_content() -> Self
pub fn partial_content() -> Self
Create a 206 Partial Content response.
Used for range requests. You should also set the Content-Range header.
§Example
use fastapi_core::{Response, ResponseBody};
let response = Response::partial_content()
.header("Content-Range", b"bytes 0-499/1000".to_vec())
.header("Accept-Ranges", b"bytes".to_vec())
.body(ResponseBody::Bytes(partial_data));Sourcepub fn range_not_satisfiable() -> Self
pub fn range_not_satisfiable() -> Self
Create a 416 Range Not Satisfiable response.
Used when a Range header specifies a range that cannot be satisfied.
You should also set the Content-Range header with the resource size.
§Example
use fastapi_core::Response;
let response = Response::range_not_satisfiable()
.header("Content-Range", b"bytes */1000".to_vec());Sourcepub fn not_modified() -> Self
pub fn not_modified() -> Self
Create a 304 Not Modified response.
Used for conditional requests where the resource has not changed. The response body is empty per HTTP spec.
Sourcepub fn precondition_failed() -> Self
pub fn precondition_failed() -> Self
Create a 412 Precondition Failed response.
Used when a conditional request’s precondition (e.g., If-Match) fails.
Sourcepub fn with_weak_etag(self, etag: impl Into<String>) -> Self
pub fn with_weak_etag(self, etag: impl Into<String>) -> Self
Set a weak ETag header on this response.
Automatically prefixes with W/ if not already present.
Sourcepub fn header(self, name: impl Into<String>, value: impl Into<Vec<u8>>) -> Self
pub fn header(self, name: impl Into<String>, value: impl Into<Vec<u8>>) -> Self
Add a header.
§Security
Header names are validated to contain only valid token characters. Header values are sanitized to prevent CRLF injection attacks. Invalid characters in names will cause the header to be silently dropped.
Sourcepub fn body(self, body: ResponseBody) -> Self
pub fn body(self, body: ResponseBody) -> Self
Set the body.
Set a cookie on the response.
Adds a Set-Cookie header with the serialized cookie value.
Multiple cookies can be set by calling this method multiple times.
§Example
use fastapi_core::{Response, Cookie, SameSite};
let response = Response::ok()
.set_cookie(Cookie::new("session", "abc123").http_only(true))
.set_cookie(Cookie::new("prefs", "dark").same_site(SameSite::Lax));Delete a cookie by setting it to expire immediately.
This sets the cookie with an empty value and Max-Age=0, which tells
the browser to remove the cookie.
§Example
use fastapi_core::Response;
let response = Response::ok()
.delete_cookie("session");Sourcepub fn status(&self) -> StatusCode
pub fn status(&self) -> StatusCode
Get the status code.
Sourcepub fn body_ref(&self) -> &ResponseBody
pub fn body_ref(&self) -> &ResponseBody
Get the body.
Sourcepub fn into_parts(self) -> (StatusCode, Vec<(String, Vec<u8>)>, ResponseBody)
pub fn into_parts(self) -> (StatusCode, Vec<(String, Vec<u8>)>, ResponseBody)
Decompose this response into its parts.
Sourcepub fn rebuild_with_headers(self, headers: Vec<(String, Vec<u8>)>) -> Self
pub fn rebuild_with_headers(self, headers: Vec<(String, Vec<u8>)>) -> Self
Rebuilds this response with the given headers, preserving status and body.
This is useful for middleware that needs to modify the response but preserve original headers.
§Example
let (status, headers, body) = response.into_parts();
// ... modify headers ...
let new_response = Response::with_status(status)
.body(body)
.rebuild_with_headers(headers);