سؤال

I'm trying to use cookies with LWP::UserAgent but they are not being saved in the text file. What am I doing wrong?

use v5.16;
use strict;
use warnings;
use Data::Dumper;

use LWP::UserAgent;
use HTTP::Cookies;
use Net::SSL ();

my $cookie_jar = HTTP::Cookies->new( 
    file => 'lwp_cookies.txt',
    autosave => 1,
);

my $ua = LWP::UserAgent->new(
    ssl_opts   => { verify_hostname => 0 },
    cookie_jar => $cookie_jar,
);

my $auth_url = 'https://my.site.url/path/authenticate';
use HTTP::Request::Common qw(GET);
my $req = HTTP::Request->new(GET => $auth_url);
$req->authorization_basic('username', 'password');

$ua->request($req);
say Dumper( \$cookie_jar );

And the dumped data looks like this:

$VAR1 = \bless( {
   'autosave' => 1,
   'COOKIES' => {
          'my.site.url' => {
                '/' => {
                     'BLAH_COOKIE_KEY' => [
                         0,
                         'very_long_string',
                         undef,
                         1,
                         undef,
                         undef,
                         1
                       ]
                   }
              }
        },
   'file' => 'lwp_cookies.txt'
 }, 'HTTP::Cookies' );

And lwp_cookies.txt just has this:

#LWP-Cookies-1.0

I'm on Win with Strawberry Perl.

هل كانت مفيدة؟

المحلول

try issuing a:

$cookie_jar->save

Also, the cookies might be expired, or requested to be discarded, so, also, construct your $cookie_jar with ignore_discard set to 1:

my $cookie_jar = HTTP::Cookies->new( 
    file => 'lwp_cookies.txt',
    autosave => 1,
    ignore_discard => 1,
);
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top