Provide a way to retrieve both the request parts and transformed body in a single extractor #1893
-
Feature RequestMotivationHey there! I'm currently trying to migrate from I used to setup my handlers with both async fn handler(parts: http::request::Parts, Json(body): Json<MyBody>) -> impl IntoResponse
{
} In From what I can see from the implementation the parts get consumed internally after a call to I've been trying to use the ProposalProvide a way to retrieve both the extracted body as well as the request parts in a single extractor. async fn handler(JsonWithParts(parts, body): JsonWithParts<MyBody>) -> impl IntoResponse
{
} AlternativesIf you see any alternative way of doing this, I am all ears :D Thanks ! |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 9 replies
-
I recommend you extract the parts you need ( |
Beta Was this translation helpful? Give feedback.
-
Just in case anyone is struggling with this on axum 0.7, here's modified accepted example for this version. pub struct Foo<T> {
matched_path: MatchedPath,
value: T,
}
#[axum::async_trait]
impl<S, T> FromRequest<S> for Foo<T>
where
S: Send + Sync,
Json<T>: FromRequest<S>
{
type Rejection = Response;
async fn from_request(req: Request, state: &S) -> Result<Self, Self::Rejection> {
let (mut parts, body) = req.into_parts();
let matched_path = MatchedPath::from_request_parts(&mut parts, state)
.await
.map_err(IntoResponse::into_response)?;
let req = Request::from_parts(parts, body);
let Json(value) = Json::<T>::from_request(req, state)
.await
.map_err(IntoResponse::into_response)?;
Ok(Foo {
matched_path,
value,
})
}
} |
Beta Was this translation helpful? Give feedback.
Parts
doesn't implementFromRequestParts
because it containsExtensions
which cannot be cloned. So we'd have to remove the extensions which would make subsequentExtension
extractors fail.I recommend you extract the parts you need (
Version
,Method
,HeaderMap
, etc) directly.