Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Pass cookie jar between LWP::UserAgent and WWW::Mechanize objects

by Dumu (Monk)
on May 17, 2016 at 10:02 UTC ( [id://1163192]=perlquestion: print w/replies, xml ) Need Help??

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

Dear Monks,

I am having problems with a Moo class I am trying to write inside a Dancer2 web application.

This web app talks to another web app (this one built with Catalyst), a copy of which is also running on my development PC.

I have seen a lot of advice to use WWW::Mechanize for any slightly more complicated web page wrangling. Now, I'm inheriting an LWP::UserAgent object from my module's parent class, which has the appropriate session cookie to log into the other web app (the Catalyst one) in order to fetch a very tiny, simple JSON document. But when I try to connect to the Catalyst web app in my code, I always just get redirected to the login page.

I expected LWP::UserAgent to just be able to get the JSON document with its transparent cookie handling facility, but it can't. I've checked and it does seem to be sending the cookie, but again, I just get the login page.

Of course, it all works fine in my browser (Firefox). It's enterprise software so I can't do anything like hard code credentials (not a great idea anyway) or grab the cookie from my Firefox store.

Is there a way to grab the cookie jar off the LWP::UserAgent object and hand it over to a WWW::Mechanize object?

Or simply to 'promote' my inherited LWP::UserAgent object to a WWW::Mechanize object?

  • Comment on Pass cookie jar between LWP::UserAgent and WWW::Mechanize objects

Replies are listed 'Best First'.
Re: Pass cookie jar between LWP::UserAgent and WWW::Mechanize objects
by Anonymous Monk on May 17, 2016 at 10:07 UTC

    I expected LWP::UserAgent to just be able to get the JSON document with its transparent cookie handling facility, but it can't. I've checked and it does seem to be sending the cookie, but again, I just get the login page.

    :) There are other headers besides cookies, even multiple cookies, webservers freely discriminate on all data supplied

    Is there a way to grab the cookie jar off the LWP::UserAgent object and hand it over to a WWW::Mechanize object?

    you could use the cookie_jar accessor, it probably won't solve your problem above

Re: Pass cookie jar between LWP::UserAgent and WWW::Mechanize objects
by Anonymous Monk on Sep 23, 2016 at 04:43 UTC
    my $mech = WWW::Mechanize->new(autocheck => 0); &log('INF',"Login IN"); my $url = "http://www.example.com/signin"; my $username = $mail; my $password = $password; my $cookie_jar = HTTP::Cookies->new(); $mech->cookie_jar($cookie_jar); $mech->get($url); my $form = $mech->form_id('form1'); $mech->field("Email", $username); $mech->field("Password", $password); $mech->click('signIn'); my $ua = LWP::UserAgent->new( cookie_jar => $cookie_jar );

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1163192]
Approved by beech
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (None)
    As of 2024-04-25 03:54 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      No recent polls found