DOKK / manpages / debian 12 / libmojolicious-perl / Mojo::UserAgent::CookieJar.3pm.en
Mojo::UserAgent::CookieJar(3pm) User Contributed Perl Documentation Mojo::UserAgent::CookieJar(3pm)

Mojo::UserAgent::CookieJar - Cookie jar for HTTP user agents

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

Mojo::UserAgent::CookieJar is a minimalistic and relaxed cookie jar used by Mojo::UserAgent, based on RFC 6265 <https://tools.ietf.org/html/rfc6265>.

Mojo::UserAgent::CookieJar implements the following attributes.

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

A callback used to decide if a cookie should be ignored by "collect".

  # 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';
  });
  my $size = $jar->max_cookie_size;
  $jar     = $jar->max_cookie_size(4096);

Maximum cookie size in bytes, defaults to 4096 (4KiB).

Mojo::UserAgent::CookieJar inherits all methods from Mojo::Base and implements the following new ones.

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

Add multiple Mojo::Cookie::Response objects to the jar.

  my $cookies = $jar->all;

Return all Mojo::Cookie::Response objects that are currently stored in the jar.

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

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

Collect response cookies from transaction.

  $jar->empty;

Empty the jar.

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

Find Mojo::Cookie::Request objects in the jar for Mojo::URL object.

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

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

Prepare request cookies for transaction.

Mojolicious, Mojolicious::Guides, <https://mojolicious.org>.

2022-12-22 perl v5.36.0