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

hi,
I have to write some very weird login script. One of the things is that I have to use LWP::UserAgent->simple_request.
I can not use the normal ->get() 'cause it follow HTTP 301 Moved permanently response, i dont want to.
The other thing is that I want to manipulate the headers in the Mech object.
For the request partucalry i have to add headers before it has been created (from thee Mech point of view).
I already done the whole thing using directly HTTP::Request/Response, but I want to integrate it inside Mech, so that i continue with with it after the login..
In pesudo code current code is like this :
$req = new HTTP:Req(GET => url1); $req->header('Cookie' => cookie1); $resp = $mech->simple_request($req); my $session = $resp->header('set-cookie'); $req2 = new HTTP:Req(GET => url1);#yes url1 again ;( $req2->header('Cookie' => $session); $req2->{_headers}->authorization_basic(user,pass); $resp2 = $mech->simple_request($req2); $req3 = new HTTP:Req(GET => url3); $req->header('Cookie' => $session); $resp3 = $mech->simple_request($req3); done..continue with $mech only..

All this doesnt feel good ;|. I want req<->resp to be stored inside the $mech some way..and to be able to continue processing with mech.

Replies are listed 'Best First'.
Re: mechanize questions ?
by davidrw (Prior) on Jun 09, 2006 at 00:44 UTC
    Looking at the WWW::Mechanize and LWP::UserAgent docs, looks like this (or these methods at least) should get you in the right direction:
    use WWW::Mechanize; my $mech = WWW::Mechanize->new(); $mech->get( $url1 ); $mech->add_header( 'Cookie' => $cookie1 ); $mech->get( $url1 ); $mech->delete_header( 'Cookie' ); $mech->add_header( 'Cookie' => $session ); $mech->default_headers->authorization_basic($browser_username,$browser +_password); $mech->get( $url3 ); ...
      that is the problem i may not use the ->get() method 'cause it follows "301 Moved permanently" and i dont want to..'cause it breaks the logic.
      That is why i'm using simple_request(), but then it requres an Request object to be passed as parameter, so i'm creating one and passing it...
      The problem with the default_headers->authorization_basic() is that is that i have a old version of libwww that dont support them (which i forgot to mention ;), sorry)
      Probably within a couple of days i will get permission to upgrade them.
      thanx for the suggestions
Re: mechanize questions ?
by planetscape (Chancellor) on Jun 09, 2006 at 03:01 UTC