解析铁中的网址并获得动态部分

时间:2017-10-13 13:23:38

标签: rust iron

我有两条铁路线:

  1. /something2/:some_str#integer
  2. :some_{int, str}#string
  3. 我如何获得这些fn my_something_int_route_handler(req: &mut Request) -> IronResult<Response> { let ref query = req.extensions.get::<Router>().unwrap().find("query").unwrap_or("/"); //what next ??? // how can I get the ":some_int" from :/something/:some_int 部分?

    fn my_something2_str_route_handler(req: &mut Request) -> IronResult<Response> {
        let ref query = req.extensions.get::<Router>().unwrap().find("query").unwrap_or("/");
    
        // how can I get the ":some_str" from :/something/:some_str
    
    {{1}}

1 个答案:

答案 0 :(得分:0)

使用router crate

let mut router = Router::new();
router.get("/users/:user_id", user_show, "user_show");
let _server = Iron::new(router).http(("127.0.0.1", 8787)).unwrap();

在处理程序中,您会获得对Router的{​​{3}}的引用,该引用可让您获取每个命名参数的值。请注意,Params::find的参数与定义路径时分配的名称相匹配:

fn user_show(req: &mut Request) -> IronResult<Response> {
    let router = req.extensions.get::<Router>()
        .expect("Unable to get router");
    user_id = router.find("user_id")
        .expect("A user id is required");
}

你有一个参数作为字符串,你Params