reqwest
is the most popular HTTP client library for Rust, offering both synchronous and asynchronous APIs for making HTTP requests. This guide will walk you through the complete installation and setup process.
Prerequisites
Before installing reqwest
, ensure you have:
- Rust installed (version 1.63 or later)
- A Rust project created with cargo new project_name
Installation Methods
Basic Installation
Add reqwest
to your Cargo.toml
file under the [dependencies]
section:
[dependencies]
reqwest = "0.12"
Installation with Features
For most use cases, you'll want to enable additional features:
[dependencies]
reqwest = { version = "0.12", features = ["json", "blocking"] }
tokio = { version = "1", features = ["full"] }
Common Feature Flags
| Feature | Description |
|---------|-------------|
| json
| JSON request/response body serialization |
| blocking
| Synchronous client API |
| cookies
| Cookie jar support |
| gzip
| Gzip response decompression |
| brotli
| Brotli response decompression |
| deflate
| Deflate response decompression |
| stream
| Streaming request/response bodies |
| rustls-tls
| Rustls TLS backend (default) |
| native-tls
| Native TLS backend |
Complete Setup Example
Here's a complete Cargo.toml
configuration for web scraping:
[package]
name = "my-scraper"
version = "0.1.0"
edition = "2021"
[dependencies]
reqwest = { version = "0.12", features = ["json", "cookies", "gzip"] }
tokio = { version = "1", features = ["full"] }
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
Building and Testing
After adding dependencies, build your project:
cargo build
Or run directly:
cargo run
Basic Usage Examples
Async GET Request
use reqwest;
use std::error::Error;
#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
let response = reqwest::get("https://httpbin.org/get")
.await?
.text()
.await?;
println!("Response: {}", response);
Ok(())
}
Synchronous Request (with blocking feature)
use reqwest::blocking;
use std::error::Error;
fn main() -> Result<(), Box<dyn Error>> {
let response = blocking::get("https://httpbin.org/get")?
.text()?;
println!("Response: {}", response);
Ok(())
}
JSON POST Request
use reqwest;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
#[derive(Serialize, Deserialize)]
struct RequestData {
name: String,
email: String,
}
#[tokio::main]
async fn main() -> Result<(), reqwest::Error> {
let client = reqwest::Client::new();
let data = RequestData {
name: "John Doe".to_string(),
email: "john@example.com".to_string(),
};
let response = client
.post("https://httpbin.org/post")
.json(&data)
.send()
.await?;
let result: HashMap<String, serde_json::Value> = response.json().await?;
println!("Response: {:#?}", result);
Ok(())
}
Client with Custom Configuration
use reqwest;
use std::time::Duration;
#[tokio::main]
async fn main() -> Result<(), reqwest::Error> {
let client = reqwest::Client::builder()
.timeout(Duration::from_secs(10))
.user_agent("My Rust Scraper 1.0")
.build()?;
let response = client
.get("https://httpbin.org/user-agent")
.send()
.await?
.text()
.await?;
println!("Response: {}", response);
Ok(())
}
Troubleshooting
Common Issues
- Compile errors about async: Ensure you have
tokio
as a dependency with appropriate features - TLS errors: Try switching between
rustls-tls
andnative-tls
features - JSON errors: Make sure you have the
json
feature enabled andserde
dependencies
Version Compatibility
Always check crates.io for the latest version. The examples above use version 0.12, but newer versions may be available.
Next Steps
Once installed, you can use reqwest
for:
- Web scraping
- API consumption
- File downloads
- Form submissions
- Custom HTTP clients
The library provides extensive documentation and examples for advanced use cases like authentication, proxies, and custom middleware.