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

I am trying to post data to a form (incuding a file upload).

The code works fine under Active Perl in windows, but I am now generating a file from a unix box.
The issue is when I run the code under unix I get a response 302 (Redirect loop detected) and the post doesn't occur.
Here is the primary code (all variables have been initialized to the proper values, and the use statements are all in place)

my $ua = LWP::UserAgent->new; # Allow post redirects to happen push @{ $ua->requests_redirectable }, 'POST'; my $result = $ua->request(POST $PostURL, Content_Type => 'form-data', Content => [ user => $UserID, password => $Password, trk_code => $TheTrackCode, upfile => [$UploadFile], ]); if ($result->is_success) { # means we got a success message back my $ReturnText = $result->content(); if (index($ReturnText, "You successfully uploaded file") > -1) { print "Upload succeeded\n"; $ReturnVal = 1; } else { print "Upload failed\n$ReturnText\n"; } } else { print "ERROR OCCURED (Code was " . $result->code() . ")\n"; my $ReturnText = $result->content(); print "Return Text was\n$ReturnText\n"; }
is_succes never gets set and I always get a 302 result even though I have added post to the requests_redirectable array.
What am I doing wrong here.
I have also tried allowing cookies just in case, but that is not the issue (I wrote the script it is attaching to and there are no cookies there)

The 302 is technically correct as it is a scriptalias cgi on apache that is being posted to, but I thought requests_redirectable was supposed to allow it to post properly.

TIA for any help you can offer. (sorry for the long post, but I wanted to give any info needed)

Replies are listed 'Best First'.
Re: HTTP form Post, Getting redirect response
by perlplexer (Hermit) on Apr 26, 2002 at 15:20 UTC
    From 'perldoc LWP::UserAgent'
    $ua->redirect_ok This method is called by request() before it tries to do any redirects. It should return a true value if a redirect is allowed to be performed. Subclasses might want to override this. The default implementation will return FALSE for POST request and TRUE for all others.
    --perlplexer
      That is what
      push @{ $ua->requests_redirectable }, 'POST';
      Is supposed to take care of
      (I have even gone in and edited the library to force a return of 1 from redirect_ok and the redirect doesn't occur,
      I have also put print output in there to make sure the push does what it is supposed to and the function is returning 1).

      Am I coding this wrong or does it just not work.
Re: HTTP form Post, Getting redirect response
by LiTinOveWeedle (Scribe) on May 23, 2002 at 11:36 UTC
    Hi man,
    redirect after post is litle bit dirty, and its not allowed in default in UserAgent.pm. But you can push things litle bit.

    Find this part in UserAgent.pm and edit like this:

    sub redirect_ok { # draft-ietf-http-v10-spec-02.ps from www.ics.uci.edu, specify: # # If the 30[12] status code is received in response to a request using # the POST method, the user agent must not automatically redirect the # request unless it can be confirmed by the user, since this might cha +nge # the conditions under which the request was issued. my($self, $request) = @_; # return 0 if $request->method eq "POST"; 1; }

    It should work now. Enjoy

    Edited:
    rob_au Gave me notice, that same can be easily done within script, just by redefining redirect_ok sub. Here is example:

    sub LWP::UserAgent::redirect_ok { 1 }

    Thanks

    Li Tin O've Weedle
    mad Tsort's philosopher