communications
communications

Reputation: 145

Perl WWW::Mechanize set cookie stored to string

i want to handle multible logins from my script, so im trying to save the actual cookies from WWW::Mechanize in a value (which works as expected), and use them again later, which wont work, heres my code:

use WWW::Mechanize;
$agent = WWW::Mechanize->new( cookie_jar => {} );
$agent->get('https://example.com/');

#save cookies to string
$cookies =  $agent->cookie_jar->as_string;
#clearing cookies
$agent->cookie_jar->clear;

#re-using the cookies (this wont work)
$agent->cookie_jar->load($cookies);

Any ideas? Thanks in advance!

Upvotes: 1

Views: 695

Answers (1)

Steffen Ullrich
Steffen Ullrich

Reputation: 123521

From the documentation of HTTP::Cookies:

   $cookie_jar->load
   $cookie_jar->load( $file )
       This method reads the cookies from the file and adds them to the $cookie_jar.  The file must be in the format written by the save() method.

You are using load with a string and not a file. Because there is no reverse method to as_string you have to save and load, not as_string and load. You might try to use just multiple cookie jars, e.g.

$old_cookies = $agent->cookie_jar;
$agent->cookie_jar({}); # new cookie jar
...
$agent->cookie_jar($old_cookies); # switch back to old cookie jar

Upvotes: 1

Related Questions