如何将许多参数传递给 rust actix_web 路由
How to pass many params to rust actix_web route
是否可以将多个参数传递给 axtic_web 路由?
// 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")
}
对于只有一个参数的路由,一切正常,但对于这个例子
我在浏览器中只看到消息:wrong number of parameters: 2 expected 1
,响应状态代码是 404。
我真的需要传递更多的参数(从一个到三个或四个)...
这非常适合 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")
}
是否可以将多个参数传递给 axtic_web 路由?
// 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")
}
对于只有一个参数的路由,一切正常,但对于这个例子
我在浏览器中只看到消息:wrong number of parameters: 2 expected 1
,响应状态代码是 404。
我真的需要传递更多的参数(从一个到三个或四个)...
这非常适合 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")
}