Skip to main content

Response

Struct Response 

Source
pub struct Response { /* private fields */ }
Expand description

HTTP response.

Implementations§

Source§

impl Response

Source

pub fn with_status(status: StatusCode) -> Self

Create a response with the given status.

Source

pub fn ok() -> Self

Create a 200 OK response.

Source

pub fn created() -> Self

Create a 201 Created response.

Source

pub fn no_content() -> Self

Create a 204 No Content response.

Source

pub fn internal_error() -> Self

Create a 500 Internal Server Error response.

Source

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));
Source

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());
Source

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.

Source

pub fn precondition_failed() -> Self

Create a 412 Precondition Failed response.

Used when a conditional request’s precondition (e.g., If-Match) fails.

Source

pub fn with_etag(self, etag: impl Into<String>) -> Self

Set the ETag header on this response.

§Example
let response = Response::ok()
    .with_etag("\"abc123\"")
    .body(b"content".to_vec());
Source

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.

Source

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.

Source

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");
Source

pub fn json<T: Serialize>(value: &T) -> Result<Self, Error>

Create a JSON response.

§Errors

Returns an error if serialization fails.

Source

pub fn status(&self) -> StatusCode

Get the status code.

Source

pub fn headers(&self) -> &[(String, Vec<u8>)]

Get the headers.

Source

pub fn body_ref(&self) -> &ResponseBody

Get the body.

Source

pub fn into_parts(self) -> (StatusCode, Vec<(String, Vec<u8>)>, ResponseBody)

Decompose this response into its parts.

Source

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);

Trait Implementations§

Source§

impl Debug for Response

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl IntoResponse for Response

Source§

fn into_response(self) -> Response

Convert into a response.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, _span: NoopSpan) -> Self

Instruments this future with a span (no-op when disabled).
Source§

fn in_current_span(self) -> Self

Instruments this future with the current span (no-op when disabled).
Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> ResponseProduces<T> for T