Fetching Data on the Internet
Explore how to use Rust's reqwest crate to fetch data from the internet with blocking and nonblocking HTTP requests. Understand how to handle responses, map JSON data into custom structs, post form data, inspect status codes, and customize headers. This lesson equips you with practical skills for web data input in Rust programming.
We'll cover the following...
We'll cover the following...
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:
[dependencies]
reqwest = { version = "0.11", features = ["blocking", ...