| Crates.io | reqwest-replay |
| lib.rs | reqwest-replay |
| version | 0.1.0 |
| created_at | 2025-07-23 23:28:38.461014+00 |
| updated_at | 2025-07-23 23:28:38.461014+00 |
| description | A library for caching HTTP responses |
| homepage | |
| repository | https://github.com/Artur-Sulej/reqwest-replay |
| max_upload_size | |
| id | 1765368 |
| size | 66,068 |
A Rust library that adds transparent request/response recording to reqwest. It remembers exact request/response pairs and returns stored responses for identical HTTP requests, making it perfect for development, testing, and reducing API costs.
reqwest with minimal code changesAdd this to your Cargo.toml:
[dependencies]
reqwest-replay = "0.1"
tokio = { version = "1.0", features = ["full"] }
use reqwest_replay::ClientBuilder;
use serde_json::json;
#[tokio::main]
async fn main() {
// Create a client with default settings (cache directory: "cache")
let client = ClientBuilder::new().build();
// First request - will be fetched from the network
let response = client
.post("https://httpbin.org/post")
.json(&json!({ "query": "example" }))
.send()
.await
.unwrap();
println!("First request status: {}", response.status());
// Identical second request - will be served from cache file
let cached_response = client
.post("https://httpbin.org/post")
.json(&json!({ "query": "example" }))
.send()
.await
.unwrap();
println!("Cached response status: {}", cached_response.status());
// Different request (different body) - will go to the network
let new_response = client
.post("https://httpbin.org/post")
.json(&json!({ "query": "different" }))
.send()
.await?;
println!("New request status: {}", new_response.status());
}
By default, cached responses are stored in a directory called cache. You can customize this location:
use reqwest_replay::ClientBuilder;
let client = ClientBuilder::new()
.cache_dir("my_custom_cache_dir") // Custom directory for cached responses
.build();
// Or use a platform-appropriate cache directory
let client = ClientBuilder::new()
.cache_dir(dirs::cache_dir().unwrap().join("my_app/cache"))
.build();
Request Hashing: When a request is made, the middleware generates a unique SHA-256 hash based on:
Lookup: The middleware checks if a cached response exists for this exact hash
Response Handling:
Storage: Responses are stored as JSON files in the specified directory, with filenames matching the request hash. Each file contains both the original request and response data, making it easy to inspect or debug.
Currently, this lib stores complete request and response data on disk, including any auth tokens or API keys. Please keep this in mind when handling sensitive data. Future versions may address this issue.
This project is licensed under the MIT License – see the LICENSE file for details.