Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re^2: Passing a cookie with LWP::UserAgent

by raflach (Pilgrim)
on May 25, 2005 at 14:07 UTC ( #460338=note: print w/replies, xml ) Need Help??


in reply to Re: Passing a cookie with LWP::UserAgent
in thread Passing a cookie with LWP::UserAgent

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?

  • Comment on Re^2: Passing a cookie with LWP::UserAgent

Replies are listed 'Best First'.
Re^3: Passing a cookie with LWP::UserAgent
by nedals (Deacon) on May 26, 2005 at 02:12 UTC

    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;
      
Re^3: Passing a cookie with LWP::UserAgent
by Anonymous Monk on May 08, 2007 at 13:30 UTC
    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^3: Passing a cookie with LWP::UserAgent
by Anonymous Monk on May 08, 2007 at 13:32 UTC
    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://460338]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in 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?