首页 > 解决方案 > 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 真的很陌生,任何帮助都将不胜感激。谢谢!

标签: apiasynchronousrustactix-webrust-actix

解决方案


推荐阅读