FromRequest

Trait FromRequest 

Source
pub trait FromRequest: Sized {
    type Error: Respondable;

    // Required method
    fn from_request(req: Request) -> Result<Self, Self::Error>;
}
Expand description

Types that can be extracted from a request.

The FromRequest trait is used for types that can be constructed from an HTTP request. Extractors implementing this trait are responsible for parsing the incoming request body, headers, or other parts of the request, and converting them into a structured type that can be used by handlers.

Since FromRequest extractors consume the request body, they can only be executed once for each handler. If your extractor doesn’t need to consume the request body (for example, when you only need access to the request headers or parts of the URL), you should implement FromRequestParts instead of FromRequest.

§Associated Types

  • Error: The type of error that can occur while extracting the request data. It must implement the Respondable trait to allow the error to be returned as a valid HTTP response.

This method extracts the data from the given HTTP request and returns a result. If the extraction is successful, it returns Ok(Self), where Self is the type implementing FromRequest. If an error occurs during extraction (e.g., due to invalid data or missing fields), it returns an error of type Self::Error, which will be transformed into an HTTP response via the Respondable trait.

§Example

use titan_core::{FromRequest, Respondable};
use titan_http::{Request, body::Body};

// A custom extractor type that implements `FromRequest`.
struct MyExtractor {
    pub field: String,
}

// Implement `FromRequest` for `MyExtractor`.
impl FromRequest for MyExtractor {
    type Error = String; // The error type we will return if extraction fails.

    fn from_request(req: Request) -> Result<Self, Self::Error> {
        // Attempt to extract the data from the request (e.g., reading the body).
        let field_value = req.uri().path().to_string(); // Example of extracting from the URL path.
        Ok(MyExtractor { field: field_value })
    }
}

async fn handler(data: MyExtractor) -> impl titan_core::Respondable { /* ... */}

// Now, `MyExtractor` can be used in a handler to extract data from the request.

Required Associated Types§

Required Methods§

Source

fn from_request(req: Request) -> Result<Self, Self::Error>

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl FromRequest for ()

Source§

impl FromRequest for String

Source§

type Error = ()

Source§

fn from_request(req: Request) -> Result<Self, Self::Error>

Source§

impl FromRequest for Request

Source§

impl<T1> FromRequest for (T1,)
where T1: FromRequest + Send,

Source§

impl<T1, T2> FromRequest for (T1, T2)

Source§

impl<T1, T2, T3> FromRequest for (T1, T2, T3)

Source§

impl<T1, T2, T3, T4> FromRequest for (T1, T2, T3, T4)

Source§

impl<T1, T2, T3, T4, T5> FromRequest for (T1, T2, T3, T4, T5)

Source§

impl<T1, T2, T3, T4, T5, T6> FromRequest for (T1, T2, T3, T4, T5, T6)

Source§

impl<T1, T2, T3, T4, T5, T6, T7> FromRequest for (T1, T2, T3, T4, T5, T6, T7)

Source§

impl<T1, T2, T3, T4, T5, T6, T7, T8> FromRequest for (T1, T2, T3, T4, T5, T6, T7, T8)

Source§

impl<T1, T2, T3, T4, T5, T6, T7, T8, T9> FromRequest for (T1, T2, T3, T4, T5, T6, T7, T8, T9)

Source§

impl<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10> FromRequest for (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10)

Source§

impl<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11> FromRequest for (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11)

Source§

impl<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12> FromRequest for (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12)

Source§

impl<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13> FromRequest for (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13)

Source§

impl<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14> FromRequest for (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14)

Source§

impl<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15> FromRequest for (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15)

Source§

impl<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16> FromRequest for (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16)

Implementors§