【发布时间】:2021-12-23 00:19:40
【问题描述】:
假设我在 rocket (v. 0.5.0-rc.1) 中有这个路由处理程序:
#[get("/route")]
pub async fn my_route() -> content::Json<String> {
let json =
rocket::serde::json::serde_json::to_string(&my_data).unwrap();
content::Json(json)
}
如何在响应中添加另一个标头(content-type: application/json 除外)?
我想过这样的事情,但它不起作用:
#[get("/route")]
pub async fn my_route() -> content::Json<String> {
let json =
rocket::serde::json::serde_json::to_string(&my_data).unwrap();
let mut content = content::Json(json);
content.set_raw_header("Cache-Control", "max-age=120");
content
}
我可以使用原始 rocket::Response 并自己设置 content-type: application/json 标头,但我无法弄清楚如何从可变长度字符串设置正文而不遇到生命周期问题。
【问题讨论】:
-
在文档中 (rocket.rs/v0.4/guide/responses) 他们说要使用
use rocket::response::content; ... fn json() -> content::Json<&'static str>
标签: rust rust-rocket