Use RequestPartsExt more in docs / examples (#1445)

* Use RequestPartsExt more in docs / examples

* Remove unused import
This commit is contained in:
Jonas Platte
2022-10-04 17:26:51 +00:00
committed by GitHub
parent a57bd9a118
commit a7d8954178
7 changed files with 31 additions and 31 deletions
@@ -6,10 +6,11 @@
//! - Complexity: Manually implementing `FromRequest` results on more complex code
use axum::{
async_trait,
extract::{rejection::JsonRejection, FromRequest, FromRequestParts, MatchedPath},
extract::{rejection::JsonRejection, FromRequest, MatchedPath},
http::Request,
http::StatusCode,
response::IntoResponse,
RequestPartsExt,
};
use serde_json::{json, Value};
@@ -32,14 +33,13 @@ where
async fn from_request(req: Request<B>, state: &S) -> Result<Self, Self::Rejection> {
let (mut parts, body) = req.into_parts();
// We can use other extractors to provide better rejection
// messages. For example, here we are using
// `axum::extract::MatchedPath` to provide a better error
// message
// We can use other extractors to provide better rejection messages.
// For example, here we are using `axum::extract::MatchedPath` to
// provide a better error message.
//
// Have to run that first since `Json::from_request` consumes
// the request
let path = MatchedPath::from_request_parts(&mut parts, state)
// Have to run that first since `Json` extraction consumes the request.
let path = parts
.extract::<MatchedPath>()
.await
.map(|path| path.as_str().to_owned())
.ok();