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;