How reqwest saves cookies from a certain request and uses them for the next time?
When I try to run example code of reqwest_cookie_store:
#[tokio::test]
async fn test_client() {
// Load an existing set of cookies, serialized as json
let cookie_store = {
let file = std::fs::File::open("tests/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() // <<< panic here
};
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 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("http://127.0.0.1:8080").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();
}
}
Got panic:
thread 'test_client' panicked at tests\test_client.rs:10:60:
called `Result::unwrap()` on an `Err` value: Error("EOF while parsing an object", line: 1, column: 1)
tests/cookie.json:
{
"session_id": "abc123"
}
From reqwest_cookie_store::CookieStore::load_json
:
NB: this method does not expect true valid JSON; it is designed to load output from the corresponding method CookieStore::save_json. For a more robust/universal JSON format, see crate::serde::json, which produces output incompatible with this method.
Maybe you're running into a case where your Json is not considered compatible?
1 Like
You are right.
I tried to run CookieStore::save_json
to know what json it creates.
In that case, cookie.json
should be something like:
{"raw_cookie":"session_id=abc123; HttpOnly; SameSite=Lax; Path=/; Expires=Thu, 03 Apr 2025 15:57:46 GMT","path":["/",true],"domain":{"HostOnly":"127.0.0.1"},"expires":{"AtUtc":"2025-04-03T15:57:46Z"}}