3

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.

Jitterbug
  • 302
  • 1
  • 11
  • what wrong with https://docs.rs/http/0.2.1/http/request/struct.Request.html#method.headers ? – Stargateur Jul 14 '20 at 00:33
  • hi @Stargateur i actually need the size of the raw response; and not just the headers only, :( – Jitterbug Jul 14 '20 at 00:35
  • I don't think hyper and request can do that, also, this is a really strange request – Stargateur Jul 14 '20 at 00:39
  • @Stargateur, not sure if what you find strange is the question to get a raw http response...? – Jitterbug Jul 14 '20 at 00:55
  • both are strange – Stargateur Jul 14 '20 at 01:02
  • hi @Stargateur, i'm saddened that you find it strange. :( this question is kinda asked in java, ajax too: [https://stackoverflow.com/questions/33282889/get-raw-http-response-with-retrofit], [https://stackoverflow.com/questions/50677103/get-raw-http-response-from-ajax] so i thought it wouldn't be any different if I ask a rust equivalent to a similar question. I am just hoping I could do it in reqwest or hyper, since I'm very new to Rust. – Jitterbug Jul 14 '20 at 01:14
  • 1
    This isn't a strange request at all, I'm also stuck looking for raw headers. Just because the problems you happen to solve don't need raw headers doesn't mean that there are no valid use cases for access to raw headers. Bummed that reqwest has this limitation that for me severely limits its usefulness. – Daniel Roethlisberger May 14 '23 at 16:31

1 Answers1

-2

@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(())
}
STEEL
  • 8,955
  • 9
  • 67
  • 89