api - Actix 请求错误,特性 `std::future::Future` 未针对 `Request<()>` 实现
问题描述
我对 Rust 很陌生,正在尝试向内部 API 发出发布请求。我收到错误:
match request.await {
^^^^^^^^^^^^^ `Request<()>` is not a future
= help: the trait `std::future::Future` is not implemented for `Request<()>`
= note: required by `poll`
在我的lib.rs
,我有:
pub async fn make_request(path: &str) -> Request<()>{
let request = Request::post(format!("http://localhost:8000/{}", path))
.body(())
.unwrap();
match request.await {
Ok(res) => println!("Response: {}", res.status()),
Err(err) => println!("Error: {}", err),
}
}
使用以下代码在模块中调用:
pub async fn get_names(req_body: String) -> impl Responder {
HttpResponse::Ok().body(req_body);
make_request("/names");
}
我知道“未来”等同于 JavaScript 承诺,所以我认为 match 语句会起作用,所以我尝试添加.await()
请求,但不确定是否必须创建一个结构来保存我将在其中实现的响应make_request
? 再说一次,我对 Rust 真的很陌生,任何帮助都将不胜感激。谢谢!
解决方案
推荐阅读
- c++ - 代理模型和按钮问题
- flutter - 返回一个未来
而不是布尔 - c# - 当两者都有约束时,从另一个泛型类派生泛型类的语法是什么?
- javascript - 在 React js 表单中预填充图像字段
- firefox - 为什么 Firefox 忽略清单?
- react-native - React Native Elements Checkbox SetState 不是函数
- sql-server - 在 MS sql server-2008 中插入 100 万行
- google-cloud-platform - Ubuntu Compute Engine VPS 上的 Google Cloud IKEv2 VPN
- javascript - 如何在对话流中使用异步函数?
- javascript - required 属性不适用于按钮