gpt4 book ai didi

rust - 如何将许多参数传递给 rust actix_web 路由

转载 作者:行者123 更新时间:2023-12-03 11:35:56 28 4
gpt4 key购买 nike

是否可以将多个参数传递给 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 ,并且响应 stauts 代码是 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")
}

关于rust - 如何将许多参数传递给 rust actix_web 路由,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59772857/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com