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

make useragent follow redirects in post actions

by Specimen (Acolyte)
on Feb 21, 2001 at 14:19 UTC ( [id://59893]=perlquestion: print w/replies, xml ) Need Help??

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

Hi
I'm trying to get the useragent to follow redirects. I have the following snippet of code:
$cookies = new HTTP::Cookies( ignore_discard => 1); $ua = LWP::UserAgent->new(); @fieldValueArray = ('this', 'that', 'andthe', 'other'); my $req = POST "http://127.0.0.1/mycgi", \@fieldValueArray;
unfortunately it doesnt follow the redirect unless its a get request (as an aside, why on earth have that behaviour ?). How can i overide the $ua->redirect_ok method so that it returns TRUE instead of FALSE ? Thanks very much, Specimen

Edit: 2001-03-03 by neshura

Replies are listed 'Best First'.
Re: make useragent follow redirects in post actions
by arhuman (Vicar) on Feb 21, 2001 at 14:43 UTC

    To answer to your question the RFC2616 says:

    If the 301 status code is received in response to a request other than GET or HEAD, the user agent MUST NOT automatically redirect the request unless it can be confirmed by the user, since this might change the conditions under which the request was issued.
    Note: When automatically redirecting a POST request after receiving a 301 status code, some existing HTTP/1.0 user agents will erroneously change it into a GET request.


    For yor other question : You can make a new MyUa.pm redefining only redirect_ok (or modified UserAgent.pm, but it's dirty) to follow your rules.
    #!/usr/bin/pl package MyUa; use LWP::UserAgent; @ISA = "LWP::UserAgent"; sub redirect_ok { # here depending on what you've decided # you return true to follow the redirect # or false to not follow # (we set up the return value depending on # other (previously set up) variable to achieve our goal) # # You can also set some variables here to to modify the way # you'll retrive the new pages elsewhere in your prog/in the LWP #(argh, dirty !) # to change POST to GET by example... } 1;

    At the office we have configured 3 different behaviour for redirect on POST :

    Strict RFC compliance->don't follow to new location.
    'Rfc erroneous' mode->follow to new location but with a get (as the RFC say you shouldn't POST automatically to the new location.
    Weird mode(but usefull)-> where we POST to the new location.

    I must precise that ALL these modes are needed as we found, in real life, sites that needs one of these modes...
      Hi
      
      Thanks for your help and info.
      Wrapping the useragent inside my own class and overriding
      the redirect_ok while I agree is purest way of doing it
      it is also the most trouble :-)
      Can't I just use some kind of nasty hack to override the 
      function though ? I thought it would be possible by 
      changing the symbol table or something like that ? (My 
      knowledge of perl down to that level is pretty sketchy)
      
      Thanks again
      
      Specimen
      Hi
      Actually, while i'd still like to know if it's possible to
      do it by directly manipulating the symbol table or something
      like that, I ended up just creating the new class at the
      top of the program with the 4 or so lines needed and used
      that one - very straightforward. Thanks for your help.
      
      Specimen
      
        OK, it's Evil, but I've done it for a quick hack once (actually, to get redirects turned off):
        use LWP::UserAgent; BEGIN { *LWP::UserAgent::redirect_ok = sub { 1 }; }
        I've got to go take a shower now. I feel so... dirty. {grin}

        -- Randal L. Schwartz, Perl hacker

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (5)
As of 2024-03-29 15:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found