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§
Sourcefn from_request(
head: &RequestHead,
body: Body,
) -> impl Future<Output = Result<Self>> + Send
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§
impl FromRequest for Request
impl<D: DeserializeOwned> FromRequest for Json<D>
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\"}"
);impl<F: Form> FromRequest for RequestForm<F>
impl<T: FromRequest> FromRequest for NoApi<T>
openapi only.