pub struct InnerPath<T>(pub T);Expand description
Extractor that will get captures from the path of the innermost router
and parse them using serde.
Any percent encoded parameters will be automatically decoded. The decoded
parameters must be valid UTF-8, otherwise InnerPath will fail and return a 400 Bad Request response.
§InnerPath vs Path
Path gives you every capture in the request path, including those matched by enclosing
nest calls. InnerPath gives you only the ones from the route the innermost router
matched. If the router isn’t nested, or is nested only at static paths, InnerPath and Path see
exactly the same captures.
§Option<InnerPath<T>> behavior
You can use Option<InnerPath<T>> as an extractor to allow the same handler to
be used in a route with parameters that deserialize to T, and another
route with no parameters at all.
§Example
These examples assume the serde feature of the uuid crate is enabled.
One InnerPath can extract multiple captures. It is not necessary (and does
not work) to give a handler more than one InnerPath argument.
use axum::{
extract::InnerPath,
routing::get,
Router,
};
use uuid::Uuid;
async fn user_info(InnerPath(user_id): InnerPath<Uuid>) {
// `user_id` is from the `{user_id}` capture,
// while `{tenant_id}` from the outer router isn't included,
// so this handler works no matter what it's nested under.
}
let api = Router::new().route("/users/{user_id}", get(user_info));
let app = Router::new().nest("/tenants/{tenant_id}", api);Captured parameters also can be deserialized into any type that implements
serde::Deserialize. This includes tuples and structs:
use axum::{
extract::InnerPath,
routing::get,
Router,
};
use serde::Deserialize;
use uuid::Uuid;
#[derive(Deserialize)]
struct Params {
user_id: Uuid,
team_id: Uuid,
}
async fn users_teams_show(
InnerPath(Params { user_id, team_id }): InnerPath<Params>,
) {
// Capture parameters will be matched by struct field names
}
async fn users_teams_create(
InnerPath((user_id, team_id)): InnerPath<(String, String)>,
) {
// When using tuples captures will be matched by their position in the route
}
let api = Router::new().route(
"/users/{user_id}/team/{team_id}",
get(users_teams_show).post(users_teams_create)
);
let app = Router::new().nest("/tenants/{tenant_id}", api);If you wish to capture all inner path parameters you can use HashMap or Vec:
use axum::{
extract::InnerPath,
routing::get,
Router,
};
use std::collections::HashMap;
async fn params_map(
InnerPath(params): InnerPath<HashMap<String, String>>,
) {
// `params` contains keys "user_id" and "team_id"
}
async fn params_vec(
InnerPath(params): InnerPath<Vec<(String, String)>>,
) {
// `params` contains one tuple for `user_id` and one for `team_id`
}
let api = Router::new()
.route("/users/{user_id}/team/{team_id}", get(params_map).post(params_vec));
let app = Router::new().nest("/tenants/{tenant_id}", api);§Providing detailed rejection output
If the URI cannot be deserialized into the target type the request will be rejected and an
error response will be returned. See customize-path-rejection for an example of how to customize that error.
Tuple Fields§
§0: T