I want to create these routes in axum:
/v1/users/:username # String
/v1/users/:user_id # u64
But creating these routes:
Router::new()
.route("/v1/users/:username", get(get_user))
.route("/v1/users/:user_id", get(get_user))
Is not possible due to this error:
thread 'main' panicked at 'Invalid route "/v1/users/:user_id": insertion failed due to conflict with previously registered route: /v1/users/:username'
How can I create the same route which accepts many different URL-Parameter types?
This doesn't even make sense on a conceptual level. Imagine you receive a request with uri path
/v1/users/12345
. How should the12345
part be interpreted? As a username, or as a numeric id? If you have some internal logic that for example requires that username must start with a letter, then you could do the following:Add a single route
route("/v1/users/:username-or-id", get(get_user))
.In
get_user
handler use Path(String) extractor.Inside
get_user
manually try to parse path segment and use different business logic depending on the result.For example:
Instead of this you could just have two separate endpoints, which probably will be clearer and harder to misuse.