FromRequest

Trait FromRequest 

Source
pub trait FromRequest: Sized {
    // Required method
    fn from_request(
        head: &RequestHead,
        body: Body,
    ) -> impl Future<Output = Result<Self>> + Send;
}
Expand description

Trait for extractors that consume the request body.

Extractors implementing this trait are used in route handlers that consume the request body and therefore can only be used once per request.

See crate::request::extractors documentation for more information about extractors.

Required Methods§

Source

fn from_request( head: &RequestHead, body: Body, ) -> impl Future<Output = Result<Self>> + Send

Extracts data from the request.

§Errors

Throws an error if the extractor fails to extract the data from the 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§

Source§

impl FromRequest for Request

Source§

impl<D: DeserializeOwned> FromRequest for Json<D>

Available on crate feature json only.

Extractor that gets the request body as JSON and deserializes it into a type T implementing serde::de::DeserializeOwned.

The content type of the request must be application/json.

§Errors

Throws an error if the content type is not application/json. Throws an error if the request body could not be read. Throws an error if the request body could not be deserialized - either because the JSON is invalid or because the deserialization to the target structure failed.

§Example

use cot::RequestHandler;
use cot::json::Json;
use cot::test::TestRequestBuilder;
use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize)]
struct MyData {
    hello: String,
}

async fn my_handler(Json(data): Json<MyData>) -> Json<MyData> {
    Json(data)
}

let request = TestRequestBuilder::get("/")
    .json(&MyData {
        hello: "world".to_string(),
    })
    .build();

assert_eq!(
    my_handler
        .handle(request)
        .await?
        .into_body()
        .into_bytes()
        .await?,
    "{\"hello\":\"world\"}"
);
Source§

impl<F: Form> FromRequest for RequestForm<F>

Source§

impl<T: FromRequest> FromRequest for NoApi<T>

Available on crate feature openapi only.