in reply to Cannot create a cookie using HTTP::Cookies

Try something newer: HTTP::CookieJar::LWP

The way forward always starts with a minimal test.

Replies are listed 'Best First'.
Re^2: Cannot create a cookie using HTTP::Cookies
by shagbark (Acolyte) on Aug 25, 2015 at 04:15 UTC
    That has no documentation at all! It doesn't even say what methods it has. It seems to expect you to go through the LWP source code and find the calls listed in the HTTP::Cookie documentation.

      No, it says:

      SYNOPSIS use LWP::UserAgent; use HTTP::CookieJar::LWP; my $ua = LWP::UserAgent->new( cookie_jar => HTTP::CookieJar::LWP->new );
      and then it says
      DESCRIPTION This module is an experimental adapter to make HTTP::CookieJar work wi +th LWP.
      So you go read the docs for HTTP::CookieJar and find that they are comprehensive. You may also need to read some of the docs for LWP::UserAgent.

      Here is a simple example:

      #! perl use strict; use warnings; use feature qw/ say /; use Data::Dumper; use LWP::UserAgent; use HTTP::CookieJar::LWP; my $protocol = 'http://'; my $domain = 'www.foobar.com'; my $path = '/baz'; my $page = '/qux.html'; my $url = join '', $protocol, $domain, $path, $page; my $file = 'cookies.txt'; ## Only here for demo. You would have a cookie file ## made somewhere else, or if not you would pass the ## values directly to HTTP::CookieJar::LWP::add() open( my $write_handle, '>', $file ) or die "open: $!\n"; print $write_handle "Secret_Number=42; Max-Age=300; Domain=$domain; Pa +th=$path\n"; close $write_handle or die "close: $!\n"; my $ua = LWP::UserAgent->new( cookie_jar => HTTP::CookieJar::LWP->new() ); open( my $read_handle, '<', $file ) or die "open: $!\n"; foreach my $cookie ( <$read_handle> ) { chomp $cookie; $ua->cookie_jar->add( $url, $cookie ); } say $ua->cookie_jar->cookie_header( $url ); say Dumper $ua->cookie_jar->cookies_for( $url ); __END__
      Output:
      [00:04][nick:~/monks]$ perl 1139766.pl Secret_Number=42 $VAR1 = { 'name' => 'Secret_Number', 'domain' => 'www.foobar.com', 'expires' => 1440486562, 'value' => '42', 'path' => '/baz', 'last_access_time' => 1440486262, 'creation_time' => 1440486262 };
      Now you just GET the URL with LWP::UserAgent in the normal way, and the cookie goes right along in the headers.

      Hope this helps!

      The way forward always starts with a minimal test.