Struct Response

Source
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

Source

pub fn new() -> Self

Create a new response with 200 OK status

Source

pub fn ok() -> Self

Create a response with 200 OK status (alias for new)

Source

pub fn with_status(status: StatusCode) -> Self

Create a response with a specific status code

Source

pub fn not_found() -> Self

Create a 404 Not Found response

Source

pub fn internal_error() -> Self

Create a 500 Internal Server Error response

Source

pub fn bad_request() -> Self

Create a 400 Bad Request 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 unauthorized() -> Self

Create a 401 Unauthorized response

Source

pub fn forbidden() -> Self

Create a 403 Forbidden response

Source

pub fn unprocessable_entity() -> Self

Create a 422 Unprocessable Entity response

Source

pub fn too_many_requests() -> Self

Create a 429 Too Many Requests response

Source

pub fn status(self, status: StatusCode) -> Self

Set the status code

Source

pub fn body<T: Into<Vec<u8>>>(self, body: T) -> Self

Set the response body from a string

Source

pub fn body_from_bytes(self, body: Vec<u8>) -> Self

Set the response body from bytes

Source

pub fn header<K, V>(self, key: K, value: V) -> Self

Set a header

Source

pub fn content_type(self, content_type: &str) -> Self

Set the Content-Type header

Source

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

Set response as JSON and serialize the value (requires “json” feature)

Source

pub fn html<T: Into<Vec<u8>>>(self, html: T) -> Self

Set response as HTML

Source

pub fn text<T: Into<Vec<u8>>>(self, text: T) -> Self

Set response as plain text

Source

pub fn redirect(status: StatusCode, location: &str) -> Self

Redirect to another URL

Source

pub fn redirect_found(location: &str) -> Self

Redirect with 302 Found status

Source

pub fn redirect_permanent(location: &str) -> Self

Redirect with 301 Moved Permanently status

Source

pub fn status_code(&self) -> StatusCode

Get the status code

Source

pub fn status_code_mut(&mut self) -> &mut StatusCode

Get a mutable reference to the status code

Source

pub fn headers(&self) -> &HeaderMap

Get the headers

Source

pub fn body_data(&self) -> &[u8]

Get the body as bytes

Source

pub fn body_bytes(&self) -> &[u8]

Get the body as bytes (alias for body_data)

Source

pub fn into_hyper_response(self) -> Response<Full<Bytes>>

Convert to hyper Response

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 Default for Response

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl From<&str> for Response

Source§

fn from(body: &str) -> Self

Converts to this type from the input type.
Source§

impl From<StatusCode> for Response

Source§

fn from(status: StatusCode) -> Self

Converts to this type from the input type.
Source§

impl From<String> for Response

Source§

fn from(body: String) -> Self

Converts to this type from the input type.
Source§

impl IntoResponse for 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: 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, 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