LWP and 302 redirects

Discussion in 'Perl Misc' started by IanW, Oct 4, 2006.

  1. IanW

    IanW Guest

    Hi

    I'm having trouble with LWP and 302 redirects. I found the
    "$ua->requests_redirectable" parameter in the LWP documentation but it still
    doesn't work. Here's my code:

    use LWP::UserAgent;
    my $ua = new LWP::UserAgent;
    push @{ $ua->requests_redirectable },'POST';
    my $request = HTTP::Request->new(POST => 'http://blah.blah');
    $request->content_type('application/x-www-form-urlencoded');
    $request->content("LOGIN1=$action&Y=$Y&p=$P");
    my $resp = $ua->request($request);

    Any ideas what I'm doing wrong?

    Ian
    IanW, Oct 4, 2006
    #1
    1. Advertising

  2. On Wed, 4 Oct 2006, Mumia W. (reading news) wrote:

    > This is from the HTTP specification at
    > ftp://ftp.isi.edu/in-notes/rfc2616.txt
    >
    > > If the 302 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.


    [ I note that this would be more at home on
    comp.infosystems.www.authoring.cgi (beware the automoderation bot),
    since there's now rather little Perl-specific about it.
    But ho hum. ]

    The behaviour of actual browsers is not necessarily in conformance
    with RFC2616, unfortunately. I have the impression that some things
    got a lot better in recent browser versions, as compared with the time
    that I originally wrote this page
    http://ppewww.ph.gla.ac.uk/~flavell/www/post-redirect
    "Redirect in response to POST transaction".

    But some users still use old browser/versions, and when you're
    configuring a server for use on the WWW, you need to make some kind of
    provision (if only to refuse service, but that's not very nice) if the
    browser does not behave itself.

    > If 'http://blah.blah' is a script that you wrote, you can return a
    > 303 (or 307 ?) status. If not, you'll have to look at the returned
    > headers to figure out where to post the form.


    My recommendation would be to configure the user-advertised URL as a
    proxy, which sends the POST transaction to the other URL, behind
    the scenes, captures the result, and feeds it back (reformatted as
    necessary) to the user. In that way, the browser has no idea what the
    actual machinery is, and there's no need to care how well the browser
    supports 30x redirection of POST request.

    Disclaimer: due to tiredness I haven't analyzed in detail what the O.P
    is trying to achieve overall. But the mention of 30x and POST
    triggered these rather general comments.

    Errata: A recent email rebuked me for writing as if "idempotent" was
    synonymous with "not producing side effects". I knew that already,
    and will be adjusting the wording to try to make that clear.

    Hope that helps a bit.
    Alan J. Flavell, Oct 4, 2006
    #2
    1. Advertising

  3. IanW

    J. Gleixner Guest

    IanW wrote:
    > Hi
    >
    > I'm having trouble with LWP and 302 redirects. I found the
    > "$ua->requests_redirectable" parameter in the LWP documentation but it still
    > doesn't work. Here's my code:


    Define "doesn't work". What is returned for the response or error?
    >
    > use LWP::UserAgent;
    > my $ua = new LWP::UserAgent;
    > push @{ $ua->requests_redirectable },'POST';
    > my $request = HTTP::Request->new(POST => 'http://blah.blah');
    > $request->content_type('application/x-www-form-urlencoded');
    > $request->content("LOGIN1=$action&Y=$Y&p=$P");
    > my $resp = $ua->request($request);
    >
    > Any ideas what I'm doing wrong?
    >
    > Ian
    >
    >
    J. Gleixner, Oct 5, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Thomas Guettler
    Replies:
    1
    Views:
    313
    Steve Holden
    Oct 26, 2004
  2. Karra
    Replies:
    2
    Views:
    754
    Octavian Rasnita
    Dec 29, 2010
  3. Alex
    Replies:
    0
    Views:
    197
  4. John Bokma

    LWP gives 302 Found after update?

    John Bokma, Aug 16, 2005, in forum: Perl Misc
    Replies:
    5
    Views:
    341
    J. Gleixner
    Aug 16, 2005
  5. Bennett Haselton
    Replies:
    2
    Views:
    185
    Peter J. Holzer
    Mar 23, 2010
Loading...

Share This Page