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

use strict; use warnings; use LWP::UserAgent; use HTTP::Cookies; # .... my declarations snipped ... my $cjar = HTTP::Cookies->new(); my $ua = LWP::UserAgent->new(keep_alive => '1', timeout => $timeout); $url = "http://$host"; $error {public_home} {url} = $url; $req = HTTP::Request->new(GET => $url); $req->content_type('application/x-www-form-urlencoded'); #$cjar->add_cookie_header($req); $t0 = time; $resp = $ua->send_request($req); $t1= time; $error {public_home} {rt} = $t1 - $t0; $ua->cookie_jar($cjar); $cjar->extract_cookies($resp); print $cjar->as_string; exit;
My expected result was to see the cookies set by the visited URL. However, $cjar appears to be empty. What have I missed?

Neil Watson
watson-wilson.ca

Replies are listed 'Best First'.
Re: Missing cookies with LWP::UserAgent and HTTP::Cookies
by borisz (Canon) on Apr 12, 2006 at 18:09 UTC
    You should add your cookie jar before the request.
    ... my $cjar = HTTP::Cookies->new(); my $ua = LWP::UserAgent->new(keep_alive => '1', timeout => $timeout); $ua->cookie_jar($cjar); my $req = HTTP::Request->new(GET => $url); my $response = $ua->request($req);
    Boris
        Then the server did not send a cookie! Here is a complete TESTED working example, that prints a cookie.
        use LWP::UserAgent; use HTTP::Cookies; my $cjar = HTTP::Cookies->new(); my $ua = LWP::UserAgent->new( keep_alive => '1', ); $ua->cookie_jar($cjar); my $req = HTTP::Request->new( GET => 'http://derstandard.at/' ); my $response = $ua->request($req); print $cjar->as_string;
        Boris