Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

actix_web How to get the response body from awc::client::ClientResponse

Tags:

rust

actix-web

I am trying to get the response out of the awc::client::ClientResponse. But always get the empty string. If I use the same request with curl I get the response. Here is what I am trying This is a actix_web service which accepts the json request and call other service to get the response. and then sends it back to the client.


async fn get_info(info: actix_web::web::Json<Info>) -> impl Responder {
    let mut builder = SslConnector::builder(SslMethod::tls()).unwrap();
    builder.set_verify(SslVerifyMode::NONE);
    let myconnector = builder.build();

    let client = Client::builder()
        .connector(Connector::new().ssl(myconnector).finish())
        .finish();
    let fund_card_req:FundingCardRequest = FundingCardRequest::new(vec![Accounts::new("45135612".to_string(), 666666)], 
        String::from("INCOMING"), 
        String::from("ACH"), 1095434517);
        println!("{:?}", serde_json::to_string(&fund_card_req));
        let mut response = client.post("https://mn.sit.jq.com/fundingcard-activity")
        .timeout(core::time::Duration::from_secs(10))
        .header("Content-Type", "application/json")
        .send_json(&fund_card_req).await.unwrap();
        match response.status(){
            StatusCode::OK => {
                println!("status: success");
                let k = response.body().limit(20_000_000).await.unwrap();
                println!("{:?}", str::from_utf8(&k)); 
            },
            _ => println!("status: failed"),

        }
    HttpResponse::Ok().body("hello world")
}

I have gone through most of the examples client examples on rust Following is my setup.

actix-web = {version ="3.3.2", features = ["openssl"]}
actix-service = "1.0.1"
openssl = "0.10.29"
like image 363
presci Avatar asked Nov 19 '25 18:11

presci


1 Answers

I know this doesn't strictly answer the question, but I decided to drop awc in favor of reqwest since it will use the operating system's TLS framework by default if you are on Windows or Mac, thus no need to set up OpenSSL or enable the feature for actix-web. It will default to OpenSSL for all other platforms. Here is an example to get the response body:

use actix_web::{get, App, HttpServer, Responder};

static CRATES: &str = "https://play.rust-lang.org/meta/crates";

#[get("/crates")]
async fn get_crates() -> impl Responder {
    reqwest::get(CRATES).await.unwrap().text().await.unwrap()
}

#[actix_web::main]
async fn main() -> std::io::Result<()> {
    HttpServer::new(|| App::new().service(get_crates))
        .bind(("127.0.0.1", 8080))?
        .run()
        .await
}
[dependencies]
actix-web = "4.2.1"
reqwest = "0.11.14"

visit: http://localhost:8080/crates

like image 200
Rokit Avatar answered Nov 21 '25 09:11

Rokit



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!