#cookie #jar #store #http #reqwest

reqwest_cookie_store

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

7 releases

0.2.0 Jun 12, 2021
0.1.5 May 12, 2021

#8 in #cookie

Download history 237/week @ 2021-10-03 329/week @ 2021-10-10 256/week @ 2021-10-17 257/week @ 2021-10-24 317/week @ 2021-10-31 289/week @ 2021-11-07 106/week @ 2021-11-14 65/week @ 2021-11-21 86/week @ 2021-11-28 64/week @ 2021-12-05 64/week @ 2021-12-12 94/week @ 2021-12-19 92/week @ 2021-12-26 104/week @ 2022-01-02 134/week @ 2022-01-09 156/week @ 2022-01-16

497 downloads per month
Used in 3 crates

MIT/Apache

11KB
104 lines

Documentation

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

Example

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.

// 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();
  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

~4.5–8.5MB
~193K SLoC