Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I print the cookie_jar values in Perl's WWW::Mechanize?

Tags:

cookies

perl

How can I print the values of the cookie/cookie_jar being set?

Trying:

##my $cookie_jar=HTTP::Cookies->new(file => "cookie.jar",autosave=>1,ignore_discard=>1);
my $cookie_jar=HTTP::Cookies->new(); ## Would like it to be in memory
my $agent = WWW::Mechanize->new(cookie_jar => $cookie_jar);

##my $agent = WWW::Mechanize->new();
##my $agent = WWW::Mechanize->new(autocheck => 1);

##$agent->cookie_jar( {} );

# we need cookies
##$agent->cookie_jar(HTTP::Cookies->new);

print "Set Cookie Jar?\n";
print $agent->cookie_jar->as_string();
print "\n";

$agent->get($url); // url is a https site

Not too much luck with any of these, what am I doing wrong?

like image 574
Phill Pafford Avatar asked Dec 29 '22 19:12

Phill Pafford


1 Answers

Well, you have to have some cookies in the cookie jar to see any cookies in the output. So far you have an empty cookie jar. Either ensure that you add some cookies or that the site you are accessing sets them:

use HTTP::Cookies;
use WWW::Mechanize;

my $cookie_jar = HTTP::Cookies->new;
my $agent      = WWW::Mechanize->new( cookie_jar => $cookie_jar );

$cookie_jar->set_cookie(
    qw(
    3
    cat
    buster
    /
    .example.com
    0
    0
    0
    )
    );

    $agent->get( 'http://www.amazon.com' );

print "Set Cookie Jar?\n", $agent->cookie_jar->as_string, "\n";

This gave me the output:

Set Cookie Jar?
Set-Cookie3: session-id=000-0000000-0000000; path="/"; domain=.amazon.com; path_spec; discard; version=0
Set-Cookie3: session-id-time=1272524400l; path="/"; domain=.amazon.com; path_spec; discard; version=0    Set-Cookie3: cat=buster; path="/"; domain=.example.com; port=0; version=3

However, you don't need to invoke HTTP::Cookies directly. LWP will take care of that. You just give cookie_jar a hash reference:

    my $agent      = WWW::Mechanize->new( cookie_jar => {} );

If you just want the cookies from a particular response, you can create a separate cookie jar to hold the ones you extract from the response:

use WWW::Mechanize;

my $agent = WWW::Mechanize->new( cookie_jar => {} );

my $response = $agent->get( 'http://www.amazon.com' );

my $cookie_jar = HTTP::Cookies->new;
$cookie_jar->extract_cookies( $response );

print $cookie_jar->as_string;
like image 98
brian d foy Avatar answered Mar 15 '23 10:03

brian d foy