Search code examples
rustactix-webscylla

How to use actix_web::client::Client in Actix-web 4.0


I use crate scylla that use tokio 1, so i must use crate actix-web 4.0 beta. Now i have problem that use actix_web::client::Client show error :

3 | use actix_web::client::Client;
  |                ^^^^^^ could not find `client` in `actix_web`

i want to hit API inside actix handler with this code :

pub(crate) async fn proses_mapmatching(data: web::Data<AppState>) -> impl Responder {

    let client = Client::default();

    let res = client.post("http://localhost:8002/trace_route")
        .send()
        .await
        .unwrap()
        .body()
        .await;

    println!("Response: {:?}", res);

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

Any idea to still use actix-web 4 with reqest post insede handler function ? Thanks

ANSWER CODE with AWC - Thanks to Mr. @kmdreko

pub(crate) async fn proses_mapmatching(data: web::Data<AppState>) -> impl Responder {

    let mut client = awc::Client::default();
    let response = client.post("http://localhost:8002/trace_route")
        .send_body("Raw body contents")
        .await;

    println!("Response: {:?}", response.unwrap().body().await);

    HttpResponse::Ok().body(format!("Hello {}!", rows.len()))
}


Solution

  • This is mentioned in the actix_web Changes.md for v4.0:

    The client mod was removed. Clients should now use awc directly.

    The actix_web::client module has long since largely been a wrapper around the awc crate ever since the 1.0 release, but it seems they now want to separate them entirely.

    The types from awc should be near identical to those exposed in prior actix_web releases, however if you're using it with actix_web:4.0 (currently in beta) then you'll want to use awc:3.0 (currently in beta) for compatibility.