| Crates.io | cookie-rs |
| lib.rs | cookie-rs |
| version | 0.4.1 |
| created_at | 2024-08-04 23:36:49.784499+00 |
| updated_at | 2025-03-03 19:50:42.941489+00 |
| description | library for working with HTTP cookies |
| homepage | |
| repository | https://github.com/magwoo/cookie-rs |
| max_upload_size | |
| id | 1325359 |
| size | 225,800 |

cookie-rs is a flexible library for working with HTTP cookies. It allows you to create, parse, and manage cookies.
Domain, Path, Secure, HttpOnly).CookieJar, which tracks additions and removals.SameSite attribute.ParseError.To use this library, add it to your dependencies:
[dependencies]
...
+ cookie-rs = "0.4.1"
use cookie_rs::prelude::*;
let cookie = Cookie::builder("session", "abc123")
.domain("example.com")
.path("/")
.secure(true)
.http_only(true)
.same_site(SameSite::Lax)
.build();
println!("{}", cookie.to_string());
Output:
session=abc123; Domain=example.com; Path=/; Secure; HttpOnly; SameSite=Lax
use cookie_rs::Cookie;
let cookie_str = "session=abc123; Secure; HttpOnly";
let cookie = Cookie::parse(cookie_str).expect("Failed to parse cookie");
assert_eq!(cookie.name(), "session");
assert_eq!(cookie.value(), "abc123");
assert_eq!(cookie.secure(), Some(true));
assert_eq!(cookie.http_only(), Some(true));
CookieJaruse cookie_rs::{Cookie, CookieJar};
let mut jar = CookieJar::default();
// Add a cookie
let cookie = Cookie::new("user", "john");
jar.add(cookie);
// Retrieve a cookie
if let Some(cookie) = jar.get("user") {
println!("Found cookie: {}={}.", cookie.name(), cookie.value());
}
// Remove a cookie
jar.remove("user");
assert!(jar.get("user").is_none());
use cookie_rs::CookieJar;
let cookie_header = "name1=value1; name2=value2";
let jar = CookieJar::parse(cookie_header).expect("Failed to parse cookies");
assert!(jar.get("name1").is_some());
assert!(jar.get("name2").is_some());
use cookie_rs::{Cookie, CookieJar};
let mut jar = CookieJar::default();
jar.add(Cookie::new("name1", "value1"));
jar.add(Cookie::new("name2", "value2"));
let headers = jar.as_header_values();
for header in headers {
println!("Set-Cookie: {}", header);
}
Output:
Set-Cookie: name1=value1
Set-Cookie: name2=value2