Mojo::U...okieJar(3)

πŸ’… Mojo::UserAgent::CookieJar β€” the manual, but make it cute

okay so bestie, pull up a chair πŸŽ€ this is Mojo::UserAgent::CookieJar, aka the actual manual name of this page, and it's the cookie jar for HTTP user agents. yes, a cookie jar. for your HTTP user agent. she's iconic for that πŸ’–


✨ SYNOPSIS ✨

here's the whole vibe in code, fr fr β€” no notes needed:

use Mojo::UserAgent::CookieJar;

# Add response cookies
my $jar = Mojo::UserAgent::CookieJar->new;
$jar->add(
  Mojo::Cookie::Response->new(
    name   => 'foo',
    value  => 'bar',
    domain => 'localhost',
    path   => '/test'
  )
);

# Find request cookies
for my $cookie (@{$jar->find(Mojo::URL->new('http://localhost/test'))}) {
  say $cookie->name;
  say $cookie->value;
}

like, add response cookies, find request cookies. the duality of cookie πŸͺ✨


🌸 DESCRIPTION 🌸

Mojo::UserAgent::CookieJar is a minimalistic and relaxed cookie jar used by Mojo::UserAgent, and it's based on RFC 6265 <https://tools.ietf.org/html/rfc6265> πŸ¦‹ yes she cites her sources, we stan.

lowkey she's chill, highkey she's correct. the way that she's minimalistic and relaxed? main character energy honestly πŸ‘‘


🌈 ATTRIBUTES 🌈

Mojo::UserAgent::CookieJar implements the following attributes. let's go 🫢

πŸŽ€ file

my $file = $jar->file;
$jar     = $jar->file('/home/sri/cookies.txt');

this is the file you "load" cookies from and "save" cookies to, in Netscape format. yes, Netscape format. she's vintage πŸ’…

# Save cookies to file
$jar->file('cookies.txt')->save;

# Empty cookie jar and load cookies from file
$jar->file('cookies.txt')->empty->load;

look at that chaining. empty then load. chef's kiss 😭

πŸ¦‹ ignore

my $ignore = $jar->ignore;
$jar       = $jar->ignore(sub {...});

a callback that decides whether a cookie should be ignored by "collect" β€” very much a vibe check for cookies if you think about it ⭐

# Ignore all cookies
$jar->ignore(sub { 1 });

# Ignore cookies for domains "com", "net" and "org"
$jar->ignore(sub ($cookie) {
  return undef unless my $domain = $cookie->domain;
  return $domain eq 'com' || $domain eq 'net' || $domain eq 'org';
});

not her rejecting the entire com, net and org domains πŸ’€ honestly fair.

my $size = $jar->max_cookie_size;
$jar     = $jar->max_cookie_size(4096);

maximum cookie size in bytes, and it defaults to 4096 (that's 4KiB bestie 🧚). size matters, she's just setting boundaries periodt.


πŸ‘‘ METHODS πŸ‘‘

Mojo::UserAgent::CookieJar inherits all methods from Mojo::Base and implements the following new ones. slay, let's get into it πŸ’…

πŸ”₯ add

$jar = $jar->add(@cookies);

add multiple Mojo::Cookie::Response objects to the jar. multiple. she's a collector.

πŸ’œ all

my $cookies = $jar->all;

return all Mojo::Cookie::Response objects that are currently stored in the jar. yes, all of them, no crumbs left behind πŸͺ

# Names of all cookies
say $_->name for @{$jar->all};

✨ collect

$jar->collect(Mojo::Transaction::HTTP->new);

collect response cookies from transaction. go little cookie, go get collected πŸ¦‹

πŸŽ€ empty

$jar = $jar->empty;

empty the jar. she's empty. she's cleansed. she's at peace 🌸

⭐ find

my $cookies = $jar->find(Mojo::URL->new);

find Mojo::Cookie::Request objects in the jar for a Mojo::URL object. a little scavenger hunt, but make it HTTP πŸ”

# Names of all cookies found
say $_->name for @{$jar->find(Mojo::URL->new('http://example.com/foo'))};

🌈 load

$jar = $jar->load;

load cookies from "file". that's it, that's the method πŸ’–

🫢 prepare

$jar->prepare(Mojo::Transaction::HTTP->new);

prepare request cookies for transaction. getting them ready, doing their makeup, sending them out πŸ’„

🧚 save

$jar = $jar->save;

save cookies to "file". persistence queen πŸ‘‘

πŸ’€ to_string

my $string = $jar->to_string;

stringify cookies in Netscape format. turn the whole jar into one string, iykyk ✨


lowkey a reminder bestie: file, ignore and max_cookie_size are attributes, and load/save literally read and write that "file" β€” so don't be weird about it πŸ’…

🌸 SEE ALSO 🌸


okay that's the whole page πŸŽ€ perl v5.42.0, dated 2026-07-15 β€” and that's genuinely the source/version and date, bestie, so now you can't say I never told you anything πŸ˜­πŸ’– bye, go set some cookies πŸͺ✨

πŸ“¦ where this page comes from