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

Guys, I am using LWP to post information from a script on one machine to a script on another box.

However, I do not know how to test for errors. My goal is to find something in the HTTP response that will tell me if the request was successful or not. $response->as_string gives me this error message that I do not understand at all:

500 (Internal Server Error) unexpected EOF before status line seen Client-Date: Thu, 14 Nov 2002 16:10:08 GMT

HOWEVER, the script is successfully called, despite the 500 error message, which makes this case even more puzzling.

Here is my code:

# HTTPIZE! foreach $elem ( keys %tform ) { $tform{$elem} = &cgi'httpize($tform{$elem}); } $qs = "Firstname=$tform{firstname}%20$tform{lastname}&Company= +$tform{company}&WorkStreetAddress=$tform{addr1}&WorkCity=$tform{city} +&WorkProvince=$tform{state}&WorkCountry=$tform{country}&WorkZipCode=$ +tform{zip}&WorkPhone=$tform{phone}&txtAddEmail=$tform{email}&signUpGS +N=T"; my $ua = new LWP::UserAgent; $ua->credentials('www.SENDER.com','outer','proactive','gr8ap3' +); $header = new HTTP::Headers; $header->header(MIME_Version => '1.0', User_Agent => 'Viaduct Proactive/2.0', From => 'proactive\@viaduct.com'); $page = 'http://www.RECEIVER.com/nti/nti.pl?'; $request = new HTTP::Request 'POST',$page,$header,$qs; $response = $ua->request ($request); $action = $response->as_string; open (TEST, ">TEST.txt") || die "ARGH!\n\n"; # THIS LINE GENERATES THE 500 ERROR! print TEST "$action\n"; # AS A RESULT, I FAIL! BOO HOO! if ($response->is_redirect) { print TEST "SUCCESS\n"; } else { print TEST "FAILED\n"; } close (TEST);
Thanks, you guys have been great in the past! Robert

Replies are listed 'Best First'.
Re: How do you find out if your HTTP response succeeded via LWP?
by Abigail-II (Bishop) on Nov 14, 2002 at 16:42 UTC
    man LWP tells you the response object has functions is_success and is_error. Is that what you need?

    Abigail

      Abigail, I tried that, and it didnt work... The problem is that I dont seem to be calling the page properly, but i dont know how to fix it. Thanks. Robert
        Abigail gave you the answer to the question you asked. Why your application is getting the 500 response is another question. One thing that I notice is that you don't seem to be sending a content-type header. Try adding Content_type => 'application/x-www-form-urlencoded'

        Also, as Rich36 suggested, I'd replace the &cgi'httpize with uri_escape.

        sorry didn't see it was already replied
Re: How do you find out if your HTTP response succeeded via LWP?
by Rich36 (Chaplain) on Nov 14, 2002 at 17:58 UTC

    Is this code running in a CGI application? If so, the error may be caused by the fact that you are trying to write to a file that you can't write to. If not, the 500 series codes refer to server errors - this may indicate problems on the remote server. Can you post the parameters manually?

    Also, you might try using URI::Escape(uri_escape) to escape your query string. There may be characters in there that are causing problems with the post.

    One thing that I usually do when running into LWP is use Data::Dumper and print out the UserAgent object ($ua). This shows the contents of the header, the retrieved document, the return code and other useful information. That might help you track down the problem.


    «Rich36»
      All is fixed! I needed the receiving script to spit out some stuff (after a Content Type text/html header). Once the script generated this text, the 500 error disappeared (HALLELUJAH!)

      Thanks for all your help on this one. robert

Re: How do you find out if your HTTP response succeeded via LWP?
by Sihal (Pilgrim) on Nov 14, 2002 at 17:13 UTC
    There is a method called is_error() that should do what you want.