reqwest_cookie_store
provides implementations of reqwest::cookie::CookieStore
for https://crates.io/crates/cookie_store
The following example demonstrates loading a cookie_store::CookieStore
from disk, and using it within a
CookieStoreMutex
. It then makes a series of requests, examining and modifying the contents
of the underlying cookie_store::CookieStore
in between.
```rust // Load an existing set of cookies, serialized as json let cookiestore = { let file = std::fs::File::open("cookies.json") .map(std::io::BufReader::new) .unwrap(); cookiestore::CookieStore::loadjson(file).unwrap() }; let cookiestore = reqwestcookiestore::CookieStoreMutex::new(cookiestore); let cookiestore = std::sync::Arc::new(cookiestore); { // Examine initial contents println!("initial load"); let store = cookiestore.lock().unwrap(); for c in store.iter_any() { println!("{:?}", c); } }
// Build a reqwest
Client, providing the deserialized store
let client = reqwest::Client::builder()
.cookieprovider(std::sync::Arc::clone(&cookiestore))
.build()
.unwrap();
// Make a sample request client.get("https://google.com").send().await.unwrap(); { // Examine the contents of the store. println!("after google.com GET"); let store = cookiestore.lock().unwrap(); for c in store.iterany() { println!("{:?}", c); } }
// Make another request from another domain println!("GET from msn"); client.get("https://msn.com").send().await.unwrap(); { // Examine the contents of the store. println!("after msn.com GET"); let mut store = cookiestore.lock().unwrap(); for c in store.iterany() { println!("{:?}", c); } // Clear the store, and examine again store.clear(); println!("after clear"); for c in store.iter_any() { println!("{:?}", c); } }
// Get some new cookies client.get("https://google.com").send().await.unwrap(); { // Write store back to disk let mut writer = std::fs::File::create("cookies2.json") .map(std::io::BufWriter::new) .unwrap(); let store = cookiestore.lock().unwrap(); store.savejson(&mut writer).unwrap(); } ```