How to get raw HttpResponse using Hyper or Reqwest?

Viewed 877

Is there a way to get the actual/original/raw http response after doing a reqwest::get("https://httpbin.org/ip").send().await?

or from hyper : client.get("https://httpbin.org/ip".parse()?).await?

I need a similar result to what postman returns:

Date: Mon, 13 Jul 2020 07:43:46 GMT
Content-Type: application/json
Content-Length: 33
Connection: keep-alive
Server: gunicorn/19.9.0
Access-Control-Allow-Origin: *
Access-Control-Allow-Credentials: true
{
  "origin": "102.200.212.40"
}

Somehow reqwest organizes the response too well; into version(), status(), headers() etc.

The reason why I specifically mentioned reqwest and/or hyper, is because these are being used extensively all around the code. I'm hoping that I could still use them, before we try another crate/lib.

1 Answers

@B.CarlaYap acttually your question is not understandable.. can you update and add an exact display of Postman call you are making. as well you are looking at HEADER which you could get from Response.

you could use Blocking which is simpler to understand.

reqwest = {version = "0.10.8", features = ["blocking", "json"]}


fn main() -> Result<(), Box<dyn std::error::Error>> {

    println!("GET https://www.rust-lang.org");

    let mut res = reqwest::blocking::get("https://www.rust-lang.org/")?;

    println!("Status: {}", res.status());
    println!("Headers:\n{:?}", res.headers());

    // copy the response body directly to stdout
    res.copy_to(&mut std::io::stdout())?;

    println!("\n\nDone.");
    Ok(())
}
Related