Skip to content

how to make a route+handler that accepts an optional value parameter? #2744

Answered by jplatte
dan-da asked this question in Q&A
Discussion options

You must be logged in to vote

In general, I would recommend that you just have two handler function that delegate most of their work to the same private function that is never used as a handler directly. However, both of the things you are asking can be achieved to some extent:

  • If you want to only call .route once to register a handler for _anything starting with /person/, you can do so with .route("/person/*rest", get(handler)); there is no syntax for limiting the number of trailing segments though
  • If you want to extract either one or two path parameters with axum::extract::Path<T>, then T has to be a type that implements Deserialize such that both inputs work, for example you could use Path<Vec<String>> for an arbi…

Replies: 2 comments 1 reply

Comment options

You must be logged in to vote
1 reply
@dan-da
Comment options

Answer selected by jplatte
Comment options

You must be logged in to vote
0 replies
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