pub trait FromRequest: Sized {
type Error: IntoResponse;
// Required method
fn from_request(req: &Request) -> Result<Self, Self::Error>;
}Expand description
Core trait for extracting typed data from HTTP requests.
Types implementing this trait can be used as handler parameters, enabling
automatic extraction and validation of request data. The framework will
automatically call from_request for each extractor parameter before
invoking the handler function.
§Type Parameters
The trait has an associated Error type that must implement Into<ExtractionError>,
allowing custom error handling for failed extractions.
§Examples
§Implementing a Custom Extractor
use ignitia::handler::extractor::FromRequest;
use ignitia::Request;
struct ApiKey(String);
impl FromRequest for ApiKey {
type Error = ExtractionError;
fn from_request(req: &Request) -> Result<Self, Self::Error> {
req.header("x-api-key")
.map(|key| ApiKey(key.to_string()))
.ok_or_else(|| ExtractionError::unauthorized("Missing API key"))
}
}
async fn protected_handler(ApiKey(key): ApiKey) -> String {
format!("Authenticated with key: {}", key)
}Required Associated Types§
Sourcetype Error: IntoResponse
type Error: IntoResponse
The error type returned when extraction fails.
Required Methods§
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.
Implementors§
Source§impl FromRequest for Multipart
impl FromRequest for Multipart
type Error = ExtractionError
Source§impl FromRequest for Body
impl FromRequest for Body
type Error = ExtractionError
Source§impl FromRequest for Cookies
impl FromRequest for Cookies
type Error = ExtractionError
Source§impl FromRequest for Headers
impl FromRequest for Headers
type Error = ExtractionError
Source§impl FromRequest for Method
impl FromRequest for Method
type Error = ExtractionError
Source§impl FromRequest for Uri
impl FromRequest for Uri
type Error = ExtractionError
Source§impl<T> FromRequest for Extension<T>
Extension extractor implementation.
impl<T> FromRequest for Extension<T>
Extension extractor implementation.
This allows extracting request extensions that were previously set by middleware or other parts of the application.