You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Actix-web 4.0找不到actix_web::client::Client的解决及POST请求实现

解决方案

Actix Web 4 起原内置的actix_web::client模块已被拆分为独立的awc(Actix Web Client)库,版本与Actix Web核心保持同步,完全兼容Tokio 1.x运行时,可满足你当前的依赖约束。

操作步骤

  1. 首先在Cargo.toml中添加awc依赖:
[dependencies]
# 保持版本与你使用的actix-web 4.x版本一致即可
awc = "4"
actix-web = "4"
  1. 修改原有代码的导入和调用逻辑即可,适配后的实现如下:
use awc::Client;
use actix_web::{web, Responder, HttpResponse};

pub(crate) async fn proses_mapmatching(data: web::Data<AppState>) -> impl Responder {
    // 初始化awc客户端
    let client = Client::default();
    // 发送POST请求,可根据需求选择send_json、send_form等方法传递不同格式的请求体
    let res = client.post("http://localhost:8002/trace_route")
        .send()
        .await
        .unwrap()
        .body()
        .await;

    println!("Response: {:?}", res);
    HttpResponse::Ok().body(format!("Hello {:?}", res))
}

额外说明

  • 若需要传递JSON请求体,可直接调用.send_json(&序列化对象)方法,无需手动设置Content-Type头
  • 生产环境建议替换unwrap()为合适的错误处理逻辑,避免请求异常时服务直接崩溃
  • awc完全复用actix-web的运行时,无需额外配置运行时即可在handler中直接调用

内容的提问来源于stack exchange,提问作者Pamungkas Jayuda

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.27 21:24:03