No description
Find a file
2024-11-02 16:29:58 +00:00
src std::sync::Mutex -> tokio::sync::Mutex 2024-11-02 16:29:58 +00:00
.gitignore initial commit 2021-05-09 20:26:55 -04:00
Cargo.toml std::sync::Mutex -> tokio::sync::Mutex 2024-11-02 16:29:58 +00:00
CHANGELOG.md chore(release): prepare for v0.8.0 2024-05-31 07:31:54 -04:00
cliff.toml ci: setup for git cliff usage 2022-11-05 15:00:02 -04:00
CONTRIBUTORS.md Update CONTRIBUTORS.md 2024-03-23 08:26:50 -04:00
LICENSE-APACHE initial commit 2021-05-09 20:26:55 -04:00
LICENSE-MIT initial commit 2021-05-09 20:26:55 -04:00
README.md re-export cookie_store::CookieStore 2022-08-29 17:57:19 -04:00
release.sh ci: remove --topo-order argument to git-cliff 2024-03-23 08:26:17 -04:00

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