Domanda

I started using the Mojolicious library for testing and everything was working fine in until I tried to extract cookies from a response.

I've tried several variants of:

$ua = Mojo::UserAgent->new();
$ua->on( error => sub { my ($ua, $error) = @_; say "This looks bad: $error"; } );
$ua->max_redirects(1)->connect_timeout(10)->request_timeout(20);
$ua->cookie_jar(Mojo::CookieJar->new);

# ... later ...

my $tx  = $ua->get($url);
my $jar = $ua->cookie_jar->extract($tx); # This is undef

I can however extract the cookies via LWP::UserAgent. However, LWP has several different issues that make that option unworkable for now. Just for a comparison here is the LWP code that does extract the cookies.

my $lwp = LWP::UserAgent->new(cookie_jar => {}, timeout => 20, max_redirect => 1);
push @{ $lwp->requests_redirectable }, 'POST';
my $response = $lwp->get($url);
die $response->status_line unless $response->is_success;
$lwp->cookie_jar->scan(\&ScanCookies);

sub ScanCookies {
   my ($version, $key, $value) = @_;
   say "$key = $value";
}

So I know that I have the $url etc. correct.

Edit: I should mention that i'm using strawberry 5.14

Edit2: I should also mention that the cookies are getting into the user agent for sure, because the session ID is getting handled properly. Unfortunately, I have a need to access another cookie (for testing the site) and I just don't seem to be able the get the right incantation to access them... saying that I believe this to be a programmer problem and nothing more.

È stato utile?

Soluzione

Use this:

$tx->res->cookies
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top