Search code examples
rustactix-web

How to pass many params to rust actix_web route


Is possible to pass more than one parameter into axtic_web route ?

// srv.rs (frag.)

HttpServer::new(|| {
  App::new()
    .route(
      "/api/ext/{name}/set/config/{id}",
      web::get().to(api::router::setExtConfig),
    )
})
.start();
// router.rs (frag.)

pub fn setExtConfig(
    name: web::Path<String>,
    id: web::Path<String>,
    _req: HttpRequest,
) -> HttpResponse {
  println!("{} {}", name, id);
  HttpResponse::Ok()
      .content_type("text/html")
      .body("OK")
}

For routes with one param everything is ok, but for this example i see only message in the browser: wrong number of parameters: 2 expected 1, and the response stauts code is 404.

I realy need to pass more parameters (from one to three or four)...


Solution

  • That is a perfect fit for tuple:

    pub fn setExtConfig(
        param: web::Path<(String, String)>,
        _req: HttpRequest,
    ) -> HttpResponse {
        println!("{} {}", param.0, param.1);
        HttpResponse::Ok().content_type("text/html").body("OK")
    }