1

I am trying to create a method un rust that could call a webserver. I have a server running on 127.0.0.1:8000. In command line, I receive an answer to curl -XGET http://127.0.0.1:8000/data and to curl -XGET http://localhost:8000/data

In rust, I have the following methods, implemented with hypper (I have the same result with reqwest, and pretty much any library I tried) :

pub async fn test() -> Result<()> {
    let res = client
        .get(Uri::from_static("http://localhost:8000/data"))
        .await.unwrap();

    println!("status: {}", res.status());

    let buf = hyper::body::to_bytes(res).await.unwrap();

    println!("body: {:?}", buf);
    Ok(())
}

Which return thread 'main' panicked at 'called Result::unwrap() on an Err value: hyper::Error(Connect, ConnectError("tcp connect error", Os { code: 111, kind: ConnectionRefused, message: "Connection refused" }))' (Note that I know I should use "?" instead of "unwrap", but my compiler never let me return a Result type with two arguments, so I unwrap it for now.)

This piece of code in particular comes from this overleaf discussion: How do I make an HTTP request from Rust?, and work as intended to contact any http url that is not my localhost.

I can't seem to how I could fix that, or even just to why it behaves that way.

cafce25
  • 15,907
  • 4
  • 25
  • 31
Antoine
  • 71
  • 1
  • 5

0 Answers0