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 theRespondabletrait 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§
type Error: Respondable
Required Methods§
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.