Fetching Data on the Internet

Learn how to fetch data from the web with Rust without fear or headaches.

The all-purpose reqwest

There are many crates and ways to fetch data from the internet in a Rust program. However, reqwest is the all-purpose tool you should have for polling the web from a Rust program. Reqwest is an HTTP client that can perform blocking and nonblocking requests.

  • Blocking requests block the program’s execution until a response has arrived. They can be easily implemented in code, but they are not very flexible.
  • Nonblocking requests, on the other hand, do not block the program. Instead, they execute some actions only when a response arrives, allowing the rest of the program to keep running in the meantime. They are not as easy to implement as blocking requests, but they are more flexible.

In order to perform blocking requests, we need to add the following dependency in our Cargo.toml:

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

Then we can, for example, write the following:

Get hands-on with 1200+ tech skills courses.