actix-web: How to create a route like this: /starts-with/a/b/c/d/.../e?

Viewed 84
#[get("/starts-with/{other_url}")]
async fn t1(other_url: web::Path<String>) -> impl Responder {
    other_url.to_string()
}

This handler works for /starts-with/a, but it doesn't work for /starts-with/a/b.

How can I create a route which works with an arbitrary number of slashes?

1 Answers

Based on this similar question and actix doc you could use the regex pattern match like

#[get("/starts-with/{other_url:.*}")]
async fn t1(other_url: web::Path<String>) -> impl Responder {
    other_url.to_string()
}
➜ curl http://127.0.0.1:8081/starts-with/x/y
Path("x/y")%
➜ curl http://127.0.0.1:8081/starts-with/x
Path("x")%
➜ curl http://127.0.0.1:8081/starts-with/x/y/z
Path("x/y/z")%

but this will require further parsing of the string.

Related