-
Heya, what changes should I do in use axum::{Router, extract::Path, routing::get, serve};
use tokio::net::TcpListener;
#[derive(serde::Deserialize)]
pub struct Something {x: i32, y: i32 }
async fn ok_route(Path((s, x, y)): Path<(String, i32, i32)>) -> String {
format!("{s} {x} {y}")
}
async fn err_route(Path((s, Something { x, y })): Path<(String, Something)>) -> String {
format!("{s} {x} {y}")
}
#[tokio::main]
async fn main() {
let router = Router::new()
.route("/ok/{s}/{x}/{y}", get(ok_route))
.route("/err/{s}/{x}/{y}", get(err_route));
let listener = TcpListener::bind("localhost:8080").await.unwrap();
serve(listener, router).await.unwrap();
} Current Behavior
Expected Behavior
Version Information
|
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 3 replies
-
That's not possible. If you use the tuple form, every element of the tuple has to correspond to one path parameter. I think the last one being |
Beta Was this translation helpful? Give feedback.
-
You can try using something else than a tuple (anything implementing #[derive(serde::Deserialize)]
pub struct Wrapper {
s: String,
#[serde(flatten)]
something: Something,
}
#[derive(serde::Deserialize)]
pub struct Something {
x: String,
y: String,
}
//...
async fn err_route(
Path(Wrapper {
s,
something: Something { x, y },
}): Path<Wrapper>,
) -> String {
format!("{s} {x} {y}")
} However, notice that I had to change |
Beta Was this translation helpful? Give feedback.
That's not possible. If you use the tuple form, every element of the tuple has to correspond to one path parameter. I think the last one being
Vec
for a wildcarm parameter works too, but not capturing multiple distinct parameters in a single tuple element.