Reqwest: Rust HTTP Client Library
Reqwest is a high-level HTTP client library that simplifies the HTTP request handling process and provides many commonly used features:
- Support for both asynchronous and blocking APIs
- Handling various types of request bodies: plain text, JSON, URL-encoded forms, multipart forms
- Customizable redirect policies
- HTTP proxy support
- TLS encryption enabled by default
- Cookie management
Basic Usage
Making a GET Request
For single requests, you can use the get convenience method:
Note: If you plan to make multiple requests, it is better to create a
Clientand reuse it to take advantage of connection pooling.
Making a POST Request
You can set the request body using the body() method:
Form Data
Sending form data is a common requirement. You can use any type that can be serialized into form data:
JSON Data
You can easily send JSON data using the json method (requires the json feature):
Response Handling
Responses can be handled in various ways:
Advanced Features
Redirect Policy
By default, the client will automatically handle HTTP redirects, following up to 10 jumps. You can customize this behavior using ClientBuilder:
Cookie Support
You can enable automatic storage and sending of session cookies via ClientBuilder:
Proxy Settings
System proxies are enabled by default, which will look for HTTP or HTTPS proxy settings in environment variables:
HTTP_PROXYorhttp_proxy: proxy for HTTP connectionsHTTPS_PROXYorhttps_proxy: proxy for HTTPS connectionsALL_PROXYorall_proxy: proxy for both types of connections
You can also explicitly set a proxy via code:
TLS Configuration
The client uses TLS by default to connect to HTTPS targets:
Timeout Settings
You can configure timeout durations for requests:
Optional Features
Reqwest provides various optional features that can be enabled or disabled via Cargo features:
http2(enabled by default): support for HTTP/2default-tls(enabled by default): provides TLS support for HTTPSrustls-tls: provides TLS functionality using rustlsblocking: provides a blocking client APIjson: provides JSON serialization and deserialization functionalitymultipart: provides multipart form functionalitycookies: provides cookie session supportgzip,brotli,deflate,zstd: support for various response body decompressionsocks: provides SOCKS5 proxy support
Blocking API
When asynchronous operations are not needed, you can use the blocking API (requires the blocking feature):