Outsiders Republic

Music. Food. Travel. Tech.

Making an HTTP GET Request with Rust

Kevin

This post describes how to implement a simple HTTP GET request with Rust using the reqwest crate. We base our example on the Make a HTTP GET request article from the Rust Cookbook. That article, though, seems to be out of date, and I could not compile the sample code as-is. It also describes how to implement synchronous GET requests. So, we’re going to improve upon the cookbook version in two ways. First, it should compile with recent versions of Rust and the reqwest crate, 1.42.x, and 0.10.x, respectively, as of this writing. Second, the resulting program makes HTTP requests asynchronously.

You can write the code on whatever platform (Mac, Linux, Windows) you want, using whatever editor you like. We compiled our code on Ubuntu 18.04 and learned that we needed to install a few dependencies. The first couple of steps describe installing those dependencies and updating Rust to the latest version.


Let’s get started!

Install dependencies

$ sudo apt-get update
$ sudo apt-get install pkg-config
$ sudo apt-get install libssl-dev

Update Rust

$ rustup update

Create a project

$ cargo new reqwest_client
$ cd reqwest_client

Update project dependencies in Cargo.toml file

# reqwest_client/Cargo.toml

[dependencies]
reqwest = "0.10.0"
tokio = { version = "0.2", features = ["full"] }

Test your setup

$ cargo run

This will compile and execute your application. If all is well, the last line of output to the terminal should be:

Hello, world!

Implement a GET request

// reqwest_client/src/main.rs

extern crate reqwest;

#[tokio::main]
async fn main() -> Result<(), reqwest::Error> {
    let client = reqwest::Client::new();
    let res = client.get("http://httpbin.org/get").send().await?;

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

    // Move and borrow value of `res`
    let body = res.text().await?;
    println!("Body:\n{}", body);

    Ok(())
}

Compile and run the program!

$ cargo run

If everything worked, then you should see output like the following:

Finished dev [unoptimized + debuginfo] target(s) in 0.23s
     Running `target/debug/reqwest_client`
Status: 200 OK
Headers:
{
    "date": "Fri, 03 Apr 2020 11:04:05 GMT",
    "content-type": "application/json",
    "content-length": "222",
    "connection": "keep-alive",
    "server": "gunicorn/19.9.0",
    "access-control-allow-origin": "*",
    "access-control-allow-credentials": "true",
}
Body:
{
  "args": {}, 
  "headers": {
    "Accept": "*/*", 
    "Host": "httpbin.org", 
    "X-Amzn-Trace-Id": "Root=1-5e871825-17e7157037417b44ae084df0"
  }, 
  "origin": "<your ip address>", 
  "url": "http://httpbin.org/get"
}

Learn more!

If you want to learn more about building HTTP clients with Rust, then check out the reqwest crate documentation and code examples. I also recommend reading up on Tokio, which is an “asynchronous run-time for the Rust programming language.” You may have noticed that we included it as a dependency in our project. That’s because reqwest and many other Rust crates depend on Tokio for async programming support.

If you found this article useful, or if you have suggestions on how to improve it, please leave a comment.



Categories: Tech

Leave a Comment

Your email address will not be published.