我正在尝试调用下游API并将结果作为Json对象返回。我使用的是使用tokio的reqwest
Cargo.toml
[dependencies]
rocket = { version = "0.5.0-rc.1", features = ["secrets", "tls", "json"] }
reqwest = { version = "0.11", features = ["blocking", "json"] }
tokio = { version = "1", features = ["full"] }main.rs
#[post("/teams")]
fn teams() -> Json<Vec<Team>> {
let mut r = reqwest::blocking::get("https://example.com");
if r.is_ok() {
return Json(get_teams(r.unwrap().text().unwrap()));
} else {
return Json(Vec::new());
}
}我得到的错误是:
thread 'rocket-worker-thread' panicked at 'Cannot drop a runtime in a context where blocking is not allowed. This happens when a runtime is dropped from within an asynchronous context.'这是因为我在使用阻塞调用吗?如何进行阻塞调用并简单地返回结果。任何指导都将不胜感激。
发布于 2021-12-03 02:54:24
这就是我所做的:
#[post("/teams")]
async fn teams() -> Json<Vec<Team>> {
return Json(get_teams(reqwest::get("https://example.com").await.unwrap().text().await.unwrap()));
}Cargo.toml
reqwest = { version = "0.11", features = ["blocking", "json"] }
tokio = { version = "1", features = ["full"] }https://stackoverflow.com/questions/69408496
复制相似问题