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

i wanted some help guys. i was sending post to http server and i wanted the response to be in json format but i get this error i dont know why.. HTTP request error, with error code 500, and body:.. this is my code

# GET JSON RESPONSE my $json = '{ "firstname": "", "lastname": "", "email": "", "country": "", }'; my $decoded = decode_json($json); # POST my $url = ''; my $ua = LWP::UserAgent->new; my $req = POST $url, Content => [ api_key => '', firstname => '', lastname => '', email => '', country => '', ]; my $resp = $ua->request($req); if ($resp->is_success) { my $resluts = $json->decode($resp->decoded_content()); my $fname = $decoded->{'firstname'}; my $lname = $decoded->{'lastname'}; my $eml = $decoded->{'email'} my $cotry = $decoded->{'country'}; } if ($resp->is_error) { die "HTTP request error, with error code ".$resp->code. ", and body:\n\n".$resp->error_as_HTML; } my ($res_code, $res_str) = split(/\|/, $resp->content); } if (! $res_code, $res_str) { #if no error #continue

Replies are listed 'Best First'.
Re: http post error (style)
by LanX (Saint) on Apr 11, 2015 at 14:35 UTC
Re: http post error
by Happy-the-monk (Canon) on Apr 11, 2015 at 12:55 UTC

    my $url = '';

    HTTP Error 500 is not a very telling error to work with. What strikes me as odd is that there is no URL used in your example code - so there should be no http happening in the first place. I understand, you edited it out?

    Cheers, Sören

    Créateur des bugs mobiles - let loose once, run everywhere.
    (hooked on the Perl Programming language)

      yes my $url ='could be any url eg yahoo.com/userapi'; i dont think there is any problem at that part. the problem must be, posting data to the http server and the way i want it to be responded back in json format

        the problem must be, posting data to the http server and the way i want it to be responded back in json format

        Then test it with a simpler approach until you get a useful result - i.e. not an Error. Continue to work in small steps from there.

        Cheers, Sören

        Créateur des bugs mobiles - let loose once, run everywhere.
        (hooked on the Perl Programming language)

Re: http post error
by pme (Monsignor) on Apr 11, 2015 at 13:15 UTC
    Hi bigup401,

    Your code even does not compile. How could you get HTTP error 500?

      it runs well, on my side, the only problem is that error i get

        A 500 response means there was an error on the server. That's all. Without seeing the server log, or getting the server to show the error in your browser, or seeing your URL so we can guess what error it might have caused, we can't even make a useful guess. It could be anything from a badly formed URL to a permissions problem to an error in a CGI script to an error in the server config -- virtually anything at all.

        Aaron B.
        Available for small or large Perl jobs and *nix system administration; see my home node.

        In this case I cannot help you unless you post what you have on your side.
Re: http post error
by Anonymous Monk on Apr 11, 2015 at 19:43 UTC
    $decoded is not $resluts which is not $results
    my $resluts = $json->decode($resp->decoded_content()); my $fname = $decoded->{'firstname'};
    500 just means the server you're talking to isn't written well, some kind of error occurred , nothing for you to guess about , its documentation reading time -- if server doesn't come with documentation contact support