4

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"
presci
  • 353
  • 1
  • 2
  • 12
  • are you getting your "status: success" message? have you checked the headers of the response, specifically the content length? – kyle Feb 23 '21 at 02:22

1 Answers1

0

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

Rokit
  • 977
  • 13
  • 23