#http-request #cookie-jar #cookies #store #jar

reqwest_cookie_store

A simple crate providing an implementation of the reqwest::cookie::CookieStore trait for cookie_store::CookieStore

12 releases (6 breaking)

0.7.0 Mar 23, 2024
0.6.0 Jun 17, 2023
0.5.0 Nov 5, 2022
0.4.0 Aug 29, 2022
0.1.5 May 12, 2021

#43 in HTTP client

Download history 992/week @ 2023-12-21 1073/week @ 2023-12-28 1722/week @ 2024-01-04 1135/week @ 2024-01-11 1154/week @ 2024-01-18 1238/week @ 2024-01-25 1284/week @ 2024-02-01 1379/week @ 2024-02-08 1569/week @ 2024-02-15 1423/week @ 2024-02-22 1868/week @ 2024-02-29 1922/week @ 2024-03-07 1728/week @ 2024-03-14 2287/week @ 2024-03-21 1934/week @ 2024-03-28 1861/week @ 2024-04-04

8,111 downloads per month
Used in 24 crates (20 directly)

MIT/Apache

15KB
147 lines

Documentation

reqwest_cookie_store provides implementations of reqwest::cookie::CookieStore for cookie_store.

Example

The following example demonstrates loading a cookie_store::CookieStore (re-exported in this crate) 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.

// Load an existing set of cookies, serialized as json
let cookie_store = {
  let file = std::fs::File::open("cookies.json")
      .map(std::io::BufReader::new)
      .unwrap();
  // use re-exported version of `CookieStore` for crate compatibility
  reqwest_cookie_store::CookieStore::load_json(file).unwrap()
};
let cookie_store = reqwest_cookie_store::CookieStoreMutex::new(cookie_store);
let cookie_store = std::sync::Arc::new(cookie_store);
{
  // Examine initial contents
  println!("initial load");
  let store = cookie_store.lock().unwrap();
  for c in store.iter_any() {
    println!("{:?}", c);
  }
}

// Build a `reqwest` Client, providing the deserialized store
let client = reqwest::Client::builder()
    .cookie_provider(std::sync::Arc::clone(&cookie_store))
    .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 = cookie_store.lock().unwrap();
  for c in store.iter_any() {
    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 = cookie_store.lock().unwrap();
  for c in store.iter_any() {
    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 = cookie_store.lock().unwrap();
  store.save_json(&mut writer).unwrap();
}

Dependencies

~5–18MB
~251K SLoC