Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: Passing a cookie with LWP::UserAgent

by nedals (Deacon)
on May 16, 2005 at 19:25 UTC ( #457534=note: print w/replies, xml ) Need Help??


in reply to Passing a cookie with LWP::UserAgent

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";

Replies are listed 'Best First'.
Re^2: Passing a cookie with LWP::UserAgent
by raflach (Pilgrim) on May 25, 2005 at 14:07 UTC

    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 :-).

        you basicly need something like this: $ua->cookie_jar(HTTP::Cookies->new(file => "cookies.txt",autosave => 1));

        some code from some of my script (to ilustrate how it works):
        $ua = LWP::UserAgent->new;
        $ua->cookie_jar(HTTP::Cookies->new(file => "cookies.txt",autosave => 1));
        my $req = HTTP::Request->new(POST => 'http://www.cacti.kobra.ktu.lt/index.php');
        $req->content_type('application/x-www-form-urlencoded');
        $req->content('login_username='.$login.'&login_password='.$passw.'&action=login');
        my $res = $ua->request($req);
        sleep(3);
        
        my $req = HTTP::Request->new(GET => 'http://www.cacti.kobra.ktu.lt/tree.php?action=item_edit&parent_id=254&tree_
        id=12&type_select=2');
        my $res = $ua->request($req);
        my $data = $res->as_string;
        
      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).
      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).

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (4)
As of 2021-05-10 08:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Perl 7 will be out ...





    Results (105 votes). Check out past polls.

    Notices?