Perl 500 internal server error. Something wrong with my code.

nitrad

New Member
Messages
1
Reaction score
0
Points
0
I get a 500 internal server error when I try to run the code below:
Code:
#! /usr/bin/perl

use LWP;

my $ua = LWP::UserAgent->new;
$ua->agent("TestApp/0.1 ");
$ua->env_proxy();

my $req = HTTP::Request->new(POST => 'http://www.google.com/loc/json');

$req->content_type('application/jsonrequest');
$req->content('{ "cell_towers": [{"location_area_code": "55000", "mobile_network_code": "95", "cell_id": "20491", "mobile_country_code": "404"}], "version": "1.1.0", "request_address": "true"}');

my $res = $ua->request($req);
if ($res->is_success) {
    print $res->content,"\n";
} else {
    print $res->status_line, "\n";
    return undef;
}

But there is no error when I run the code below:
Code:
#! /usr/bin/perl

use CGI::Carp qw(fatalsToBrowser);
 

print "Content-type: text/html\n\n";
print "<HTML>\n";
print "<HEAD><TITLE>Hello World!</TITLE></HEAD>\n";
print "<BODY>\n";
print "<H2>Hello World!</H2> <br /> \n";
 
foreach $key (sort keys(%ENV)) {
  print "$key = $ENV{$key}<p>" ;
}
print "</BODY>\n";
print "</HTML>\n";
So I think there is some problem with my code. When I run the first perl script in my local machine with the -wc command, it says that the syntax is OK. Help me please.
 
Last edited:

ZymeX

New Member
Messages
6
Reaction score
0
Points
0
Are your page properties set correctly? Most perl scripts either have pages set at 755 or 777 but it all depends on the script. So try doing that first and if it doesn't work let me know.
 

misson

Community Paragon
Community Support
Messages
2,572
Reaction score
72
Points
48
One issue is you're not printing any response headers, so the response body is treated as the response headers. Since the body isn't valid as headers, you're getting a 500 error. Try adding:

Code:
use CGI ':standard';
...
if ($res->is_success) {
    print header(-type => $res->header('Content-type'));
    print $res->content,"\n";
} else {
    print header(-status => $res->status_line);
    # should print an error response body 
    return undef;
}
 
Top