Reqwest — Ergonomic HTTP Client for Rust
Reqwest is the most popular HTTP client library for Rust. It provides a high-level, async API for making HTTP requests with automatic JSON serialization, TLS, cookie management, proxy support, and connection pooling — the Rust equivalent of Python requests.
这个资产会安全暂存
这个资产会先安全暂存。复制的指令会要求 Agent 读取暂存文件,并在激活脚本、MCP 配置或全局配置前先确认。
npx -y tokrepo@latest install 82b3259d-3745-11f1-9bc6-00163e2b0d79 --target codex先暂存文件;激活前需要读取暂存 README 和安装计划。
What it is
Reqwest is the most widely used HTTP client library for Rust. It provides a high-level, async-first API for making HTTP requests with automatic JSON serialization via serde, native TLS or rustls, cookie management, proxy support, connection pooling, and redirect handling. It sits on top of hyper for the HTTP layer and tokio for async runtime.
Reqwest targets Rust developers building API clients, web scrapers, microservice integrations, or any application that needs to make HTTP calls. It is the Rust equivalent of Python's requests library.
How it saves time or tokens
Using hyper directly requires manually handling connection management, TLS configuration, body streaming, and response parsing. Reqwest wraps all of this in a clean API. A JSON POST request that would take 20+ lines with hyper takes 3 lines with reqwest. The built-in connection pool reuses TCP connections automatically, improving throughput without extra code.
How to use
- Add reqwest with JSON support:
cargo add reqwest --features jsonand add tokio:cargo add tokio --features full. - Create a
reqwest::Client(reuse it across requests for connection pooling). - Call
.get(),.post(),.json(), etc. and.awaitthe response.
Example
use reqwest;
use serde::Deserialize;
#[derive(Deserialize, Debug)]
struct User {
login: String,
id: u64,
public_repos: u32,
}
#[tokio::main]
async fn main() -> Result<(), reqwest::Error> {
// Simple GET
let body = reqwest::get('https://httpbin.org/get')
.await?
.text()
.await?;
println!('Body: {}', &body[..100]);
// Typed JSON response
let user: User = reqwest::get('https://api.github.com/users/rust-lang')
.await?
.json()
.await?;
println!('User: {:?}', user);
Ok(())
}
Related on TokRepo
- Coding Tools -- Developer libraries and utilities
- Web Scraping Tools -- HTTP clients and scraping frameworks
Common pitfalls
- Creating a new
reqwest::Clientper request instead of reusing one. Each client creates its own connection pool and TLS sessions, wasting resources. - Forgetting to enable the
jsonfeature flag and getting compile errors when calling.json()on responses. - Using the blocking client (
reqwest::blocking) inside an async runtime, which panics. Use the async API within tokio/async-std contexts.
常见问题
Yes. Reqwest supports HTTP/2 via the h2 crate. When you connect to an HTTPS endpoint that supports HTTP/2, reqwest negotiates it automatically via ALPN. You can also force HTTP/2 with the client builder. HTTP/2 multiplexes multiple requests over a single connection, reducing latency.
Hyper is a low-level HTTP library that handles the protocol layer. Reqwest is a high-level client built on hyper that adds connection pooling, cookies, redirects, JSON serialization, and a simpler API. Use hyper when you need fine-grained control over the HTTP layer; use reqwest for everyday HTTP requests.
Reqwest provides a `blocking` module that wraps the async client in a synchronous API. This does not require tokio in your own code (reqwest spawns a runtime internally). However, the async API with tokio is the recommended approach for production applications.
By default, reqwest uses the platform's native TLS library (OpenSSL on Linux, Secure Transport on macOS, SChannel on Windows). You can switch to rustls (a pure-Rust TLS implementation) via the `rustls-tls` feature flag. Rustls avoids C dependencies and simplifies cross-compilation.
Yes. Use `.bytes_stream()` on a response to get a `Stream<Item = Result<Bytes>>` that you can process chunk by chunk. This is essential for downloading large files or processing server-sent events without loading the entire response into memory.
引用来源 (3)
- Reqwest GitHub— Reqwest is built on hyper and tokio for async HTTP
- Reqwest Documentation— Supports native-tls and rustls backends
- Hyper Documentation— Connection pooling and HTTP/2 support via hyper
讨论
相关资产
HTTPX — The Next-Generation HTTP Client for Python
HTTPX provides the ergonomics of requests with the power of async, HTTP/2, and strict type safety. The same API works in sync and async code, making it the go-to client for modern Python applications.
OkHttp — Modern HTTP Client for Java and Kotlin
A reliable and efficient HTTP client for the JVM and Android with connection pooling, transparent GZIP, response caching, and WebSocket support.
Undici — High-Performance HTTP Client for Node.js
The official HTTP/1.1 client for Node.js, written from scratch to provide a faster, more correct, and fully featured alternative to the legacy http module.
Got — Human-Friendly HTTP Client for Node.js
Got is a lightweight, feature-rich HTTP client for Node.js with built-in retry logic, pagination, caching, and hooks for composable request pipelines.