Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Passing a cookie with LWP::UserAgent

by nedals (Deacon)
on May 13, 2005 at 06:32 UTC ( #456612=perlquestion: print w/replies, xml ) Need Help??

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

I understand how to pass 'get' and 'post' data using LWP::UserAgent, but I need to pass a cookie. I read through CPAN's LWP and HTTP::Cookies and many of the posts on PM, but I'm just not getting it.

I see things like this that require a file to store cookies.

$cookie_jar = HTTP::Cookies->new(file => "$ENV{'HOME'}/lwp_cookies.dat +')

All I want to do is pass on a known cookie. So I tried this but it does not work.

use strict use CGI use LWP::UserAgent; ## Get cookie already set on browser. my $q = new CGI; my $value = $q->cookie('name'); my $ua = LWP::UserAgent->new; $ua->cookie_jar({ 'name'=> $value }); my $response = $ua->get('http://domain.com/cgi-bin/stats.cgi'); my $stats = ($response->is_success) ? $response->content : '';

Replies are listed 'Best First'.
Re: Passing a cookie with LWP::UserAgent
by arc_of_descent (Hermit) on May 13, 2005 at 09:02 UTC

    My guess is that you're not creating the cookie jar in the right manner. I don't think it accepts "name => value" pairs. You need to create a HTTP::Cookies object and use that in LWP's cookie_jar method. Then you can maybe use set_cookie to create a cookie.

Re: Passing a cookie with LWP::UserAgent
by tphyahoo (Vicar) on May 13, 2005 at 12:32 UTC
    I wound up with this code a while ago when I was troubleshooting some issues with cookies, logging into a google adwords account. I did eventually get it to work. YMMV...
    my $cookie_jar = HTTP::Cookies->new; $cookie_jar->clear; # shouldn't be necessary since object was newl +y created in above line but this is all experimentation... # A variable that holds our POST request to the site $ENV{HTTPS_VERSION} = 3; my $action = POST 'https://adwords.google.com/select/LoginValidati +on', [ 'hl' => $language, 'login.userid' => $adwordsEmailLogin, 'login.password' => $adwordsPassword, 'login' => 'Login' ]; my $ua = LWP::UserAgent->new; $ua->cookie_jar($cookie_jar); # We disguise ourselves as Internet Explorer $ua->agent('Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)'); $ua->timeout(30); #$ENV{HTTPS_PROXY} = 'http://proxy.host:proxy_port'; #if need prox +y if ($proxy) { logMessage( "adwordsLogin: Logging into adwords using proxy $p +roxy.\n"); $ua->proxy('http',$proxy); # should this be https? No, seems +to work as is. # actually, not sure. check this later. } my $www = $ua->request( $action ); # In the event that the request to google fails my $headers = new HTTP::Headers; $headers = $www->headers(); my $redir = $headers->header('Location'); #target url my $action2 = GET 'https://adwords.google.com'.$redir; my $www2 = $ua->request( $action2 ); unless ($www2->is_success) { logMessage( "AdwordsLogin: The error code: " . $www2->code . " + was returned!"); } $cookie_jar->extract_cookies($www2); # Put the HTML into a seperate variable #my $content = $www2->content; # test #open F, "> adwordsLoginResult.html"; print F $content; close F; $adwordsLoginTries++; return \$cookie_jar;
Re: Passing a cookie with LWP::UserAgent
by thcsoft (Monk) on May 13, 2005 at 09:11 UTC
    are you working on localhost? then you will always run into troubles with cookies: the server (apache) sends them, but some browsers don't send them back(e.g. opera), others (e.g. firefox) not even accept them.

    language is a virus from outer space.
Re: Passing a cookie with LWP::UserAgent
by nedals (Deacon) on May 16, 2005 at 19:25 UTC

    I'm slowly going nuts!
    Either I'm making this much too difficult or, more likely, I just don't get it. I've read though the above posts and I'm still missing it.

    I have a number of .cgi web-pages that use a cookie for user authentication and I sometimes want to access these via LWP (which needs to pass the cookie.)

    Here's my latest, not working, version. It runs ok, but it's not passing the cookie.

    use strict; use CGI; use LWP::UserAgent; use HTTP::Cookies; my $q = new CGI; my $cookie_value = $q->cookie('name'); # Get cookie from bowser my $ua = LWP::UserAgent->new; my $cookie_jar = HTTP::Cookies->new; # Pass cookie in LWP. my $version = ''; # (from CPAN) What's this? $cookie_jar -> set_cookie($version,'name',$cookie_value,"/"); $ua -> cookie_jar($cookie_jar); my $form_data = [ name => 'value', ]; my $response = $ua->post("http://domain.com/cgi-bin/script.cgi",$form_ +data); $html = ($response->is_success) ? $response->content : "Page not respo +nding";

      I'm attempting to do exactly the same thing with no luck.

      To clarify: I have a cgi script which attempts to access another cgi script via lwp. The second script does userauthentication using a cookie. So does the first one. Both use the same cookie for authentication. So what I need to do is extract the cookie from the cgi in the first script so that I can pass it to through the HTTP::Request to the second script.

      The above code looks like it should work, but doesn't. Can anyone offer advice on this?

        Hi raflach,
        I struggled with this for a couple more hours and then went to plan 'B'

        What I did was to get the cookie value in my initial script and pass it, via a post, to the LWP'd script. I modified my authenication subroutine (saved in a module), to accept first a cookie and, if that failed, get the cookie value from the posted data.

        This works well and I do not have to worry about passing cookies. It also has an added bonus. I am now able to run my scripts if cookies are disabled by saving the cookie value in a hidden field.

        Some day I'll understand how this cookie_jar thing works :-).

        Well, I was trying to do the same thing. Had lots of trouble. Couldn't figure out why until I realized that the file I was trying to initialize with wasn't being loaded.
        my $ua = LWP::UserAgent->new; $ua->cookie_jar(HTTP::Cookies::Netscape->new('file' => '<your cookie f +ile here>' )); my $url = URI->new( '<your URL here>' ); my $response = $ua->request(HTTP::Request->new(GET => $url));
        but the above code works. If you follow other examples, you'll end up creating a seperate "cookie jar" and attaching it to your user agent. Just make sure your cookie file is being read because nothing really lets you know it didn't work (I'm sure there is a status or something, but I didn't get into the details and it didn't die or anything on its own).
        Well, I was trying to do the same thing. Had lots of trouble. Couldn't figure out why until I realized that the file I was trying to initialize wasn't being loaded.
        my $ua = LWP::UserAgent->new; $ua->cookie_jar(HTTP::Cookies::Netscape->new('file' => '<your cookie f +ile here>' )); my $url = URI->new( '<your URL here>' ); my $response = $ua->request(HTTP::Request->new(GET => $url));
        but the above code works. If you follow other examples, you'll end up creating a seperate "cookie jar" and attaching it to your user agent. Just make sure your cookie file is being read because nothing really lets you know it didn't work (I'm sure there is a status or something, but I didn't get into the details and it didn't die or anything on its own).
Re: Passing a cookie with LWP::UserAgent
by inman (Curate) on May 13, 2005 at 19:08 UTC
    Try using one of the HTTP::Cookies derivatives such as HTTP::Cookies::Netscape or HTTP::Cookies::Microsoft depending on the browser that you are using. You can pre-populate cookies using your browser and then use them in your script.

    Remember that session cookies will be deleted when a browser session ends. It may be the case that you need to get your script to do the login part in order to generate the session cookies that are later used in the script. Check out WWW::Mechanize as a means of logging in using a form etc.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (4)
As of 2021-04-20 04:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?