Search code examples
rustnickel

How to use multiple variables in routes with Nickel?


Nickel states that you can use variables in the URLs, which sounds very useful, but is it possible to use multiple variables?

Something like:

www.example.com/login/:userid?:apikey?:etc

server.get("/start/:userid?:passwd", middleware! { |request|
    // format!("This is user: {:?} = {:?}",
    // request.param("userid"),
    // request.param("passwd")
    // );
});

Solution

  • You need a separator. For example:

    #[macro_use] extern crate nickel;
    
    use nickel::Nickel;
    
    fn main() {
        let mut server = Nickel::new();
    
        server.utilize(router! {
            get "/start/:userid/:passwd" => |request, _response| {
                println!("this is user: {:?} = {:?}",
                         request.param("userid"),
                         request.param("passwd")
                        );
    
                "Hello world!"
            }
        });
    
        server.listen("127.0.0.1:6767");
    }
    

    It looks from your question like you might be expecting passwd as some sort of query parameter, rather than in the URL, though.

    I would caution you against creating a session with GET, and you should be using POST instead.