Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Cannot create a cookie using HTTP::Cookies

by shagbark (Acolyte)
on Aug 25, 2015 at 02:21 UTC ( [id://1139760]=perlquestion: print w/replies, xml ) Need Help??

shagbark has asked for the wisdom of the Perl Monks concerning the following question:

LWP::UserAgent uses HTTP::Cookies to create cookies. HTTP::Cookies has 4 ways for you to load a cookie:

1. Load it from an LWP-format file, a format known as "Set-Cookie3". This is impractical, since the format is not documented anywhere that I can find.

2. Set it using HTTP::Cookies->set_cookie( $version, $key, $val, $path, $domain, $port, $path_spec, $secure, $maxage, $discard, \%rest ). The documentation says,

"The set_cookie() method updates the state of the $cookie_jar. The $key, $val, $domain, $port and $path arguments are strings. The $path_spec, $secure, $discard arguments are boolean values. The $maxage value is a number indicating number of seconds that this cookie will live. A value <= 0 will delete this cookie. %rest defines various other attributes like "Comment" and "CommentURL".

To use this, I would need to know what the arguments $path_spec and $discard mean, what $version should be set to, and what to set $port to if I want the cookie to work for any port, as cookies generally do.

3. Create the cookie in Firefox and load it from the resulting Netscape-format file. This is impossible, since Firefox and Chrome now both store cookies in sqlite.

4. Create a Netscape cookie by hand in a file, using the file format given here: http://www.cookiecentral.com/faq/#3.5

Then load this in like so: my $cookieJar = HTTP::Cookies::Netscape->new(file => 'cookies.netscape');

When I do this, with the file

.foo.net\tTRUE\t/\tFALSE\t1755828131\tcookie_name\ttrue
where \t is a tab, I get the error message
cookies.netscape does not look like a netscape cookies file at /usr/li +b/perl5/site_perl/5.14/HTTP/Cookies/Netscape.pm line 21, <FILE> line +1.

How do you create a cookie to use with LWP::UserAgent?

Replies are listed 'Best First'.
Re: Cannot create a cookie using HTTP::Cookies
by 1nickt (Canon) on Aug 25, 2015 at 03:51 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.
Re: Cannot create a cookie using HTTP::Cookies
by shagbark (Acolyte) on Aug 25, 2015 at 02:35 UTC

    Looking at the source code for HTTP::Cookies,

    $version defaults to 0, but in other code, it defaults to 1 if $discard or $secure are set, but only if it hasn't already defaulted to zero?

    $path_spec is optional?, and adds a "\$Path="$path" line to the HTTP header. I don't know what this does.

    It appears that setting $discard causes the cookie to be deleted at some time; perhaps end of session?

    It is not clear at all how $port works, but it seems to be optional

    Also, googling shows other people have asked what the documentation means, and the module's creator replied by saying he didn't see how it could be made any clearer.

    Thanks for the module, but that part of the documentation could be made clearer.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1139760]
Approved by kcott
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (5)
As of 2024-04-23 11:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found