Skip to content
Discussion options

You must be logged in to vote

I think the easiest way is to define a struct with the params you want:

#[tokio::main]
async fn main() {
    let app = Router::new()
        .route("/:a", get(handler_a))
        .route("/:a/:b", get(handler_b));

    axum::Server::bind(&"0.0.0.0:3000".parse().unwrap())
        .serve(app.into_make_service())
        .await
        .unwrap();
}

#[derive(Debug, Deserialize)]
struct Params {
    // this field name must correspond to the path param (`:a`)
    // params with other names will be ignored by `Path`
    a: String,
}

async fn handler_a(Path(params): Path<Params>) {
    dbg!(params);
}

async fn handler_b(Path(params): Path<Params>) {
    dbg!(params);
}

But you can also extract P…

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@qihexiang
Comment options

Answer selected by qihexiang
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants