Skip to main content

FromRequest

Trait FromRequest 

Source
pub trait FromRequest: Sized {
    // Required method
    fn from_request(
        req: &mut Request,
    ) -> impl Future<Output = Result<Self, ApiError>> + Send;
}
Expand description

Trait for extracting data from the full request (including body)

This is used for extractors that consume the request body.

§Example: Implementing a custom extractor that consumes the body

use rustapi_core::FromRequest;
use rustapi_core::{Request, ApiError, Result};
use std::future::Future;

struct PlainText(String);

impl FromRequest for PlainText {
    async fn from_request(req: &mut Request) -> Result<Self> {
        // Ensure body is loaded
        req.load_body().await?;
         
        // Consume the body
        if let Some(bytes) = req.take_body() {
            if let Ok(text) = String::from_utf8(bytes.to_vec()) {
                return Ok(PlainText(text));
            }
        }
         
        Err(ApiError::bad_request("Invalid plain text body"))
    }
}

Required Methods§

Source

fn from_request( req: &mut Request, ) -> impl Future<Output = Result<Self, ApiError>> + Send

Extract from the full request

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§