salvo-rs/salvo

Can't get `**rest_path`

Closed this issue · 4 comments

Describe the bug
Can't get **rest_path

To Reproduce
Steps to reproduce the behavior:

  1. Create an example-path in examples folder.
  2. Insert this into Cargo.toml:
[package]
name = "example-path"
version.workspace = true
edition.workspace = true
publish.workspace = true

[dependencies]
salvo = { workspace = true }
tokio = { workspace = true, features = ["macros"] }
tracing.workspace = true
tracing-subscriber.workspace = true

and this into main.rs:

use salvo::prelude::*;

#[handler]
async fn hello(req: &Request) -> &'static str {
    let rest_path = req.param::<String>("**rest_path").ok_or("Cannot get rest path.").unwrap();
    "Hello World"
}

#[tokio::main]
async fn main() {
    tracing_subscriber::fmt().init();

    // only allow access from http://localhost:5800/, http://0.0.0.0:5800/ will get not found page.
    let router = Router::with_path("assets/<**rest_path>")
        .get(hello);

    let acceptor = TcpListener::new("0.0.0.0:5800").bind().await;
    Server::new(acceptor).serve(router).await;
}
  1. Run: cargo run --bin example-path
  2. Create a request: curl localhost:5800/assets/wherever
  3. Get an error:
thread 'tokio-runtime-worker' panicked at path/src/main.rs:5:101:
called `Result::unwrap()` on an `Err` value: "Cannot get rest path."
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

Expected behavior
No error.

Desktop (please complete the following information):

  • OS: Arch Linux
  • Browser: Chrome, Firefox, curl

Change this line to:

 let rest_path = req.param::<String>("rest_path").ok_or("Cannot get rest path.").unwrap();

In old version salvo use **rest_path to get the rest path, but **rest_path is not a valid variable name and would cause problems when parsing OpenAPI, so we made a change.

Ok, thanks!
Then docs in the site are needed to change too (https://salvo.rs/book/concepts/router.html#path-filter)

Thanks for your reminder, the document has been fixed.