3

I get a 500 internal server error when I try to run the code below in a web server which supports perl:

#! /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:

#! /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.

brian d foy
  • 129,424
  • 31
  • 207
  • 592
Nitish
  • 1,686
  • 8
  • 23
  • 42
  • 1
    When you have trouble with a Perl CGI script, go through my "Troubleshooting Perl CGI Script": http://stackoverflow.com/questions/2165022/how-can-i-troubleshoot-my-perl-cgi-script – brian d foy May 21 '10 at 00:26
  • I don't know whether this helps but I got my perl-cgi script working after I put shebang line `#!C:\Strawberry\perl\bin\perl` –  Mar 11 '12 at 14:42

4 Answers4

5

I assume you're running the first script as a CGI script? You need to include the content type:

print "Content-type: text/plain\n\n";

before any other output (change text/plain to text/html or whatever is appropriate, of course!)

psmears
  • 26,070
  • 4
  • 40
  • 48
0

use what you are using in other script.

print "Content-type: text/html\n\n";
print "<HTML>\n";

Also, Look at CGI Programming for writing a healthy script.

Space
  • 7,049
  • 6
  • 49
  • 68
0

If all you're seeing is the "500 Server Error" page, even with use CGI::Carp qw(FatalsToBrowser), then the web server is failing to start your Perl program - it's in the wrong place, perl isn't at /usr/bin/perl, the directory doesn't have cgi execution enabled, or whatever.

The general rule when you get a 500 error is to look in the web server's error log to see what the actual error message was.

Edit: Just re-read the question and realized I was looking at the "working" code rather than the "not working" code. That changes the list of potential problem points, and the earlier answer that any CGI executable must return a content-type (even if it returns nothing else) is a likely cause of the problem, but my original take-home message is unaffected: "500 Internal Server Error" means "check the server error log".

Dave Sherohman
  • 45,363
  • 14
  • 64
  • 102
0

500 internal server error

may be caused by incorrect permissions: try CHMOD 755 to the files/folders you want to execute..

T.Todua
  • 53,146
  • 19
  • 236
  • 237