我使用Rocket库,我需要创建一个端点,其中包含动态参数"type",一个关键字.
我试过这样的东西,但它没有编译:
#[get("/offers?<type>")]
pub fn offers_get(type: String) -> Status {
unimplemented!()
}
Run Code Online (Sandbox Code Playgroud)
编译器错误:
error: expected argument name, found keyword `type`
Run Code Online (Sandbox Code Playgroud)
是否有可能在火箭中有一个名为"type"的参数?由于我遵循的规范,我无法重命名参数.
与保留关键字相同的命名查询参数存在已知限制.它在字段重命名主题的文档中突出显示.它确实提到了如何使用一些额外的代码来解决您的问题.用例示例:
use rocket::request::Form;
#[derive(FromForm)]
struct External {
#[form(field = "type")]
api_type: String
}
#[get("/offers?<ext..>")]
fn offers_get(ext: Form<External>) -> String {
format!("type: '{}'", ext.api_type)
}
Run Code Online (Sandbox Code Playgroud)
对于GET请求/offers?type=Hello,%20World!它应该返回type: 'Hello, World!'