Why is my Perl CGI script causing an internal server 500 error?

I get an internal internal server 500 error when I try to run the code below on a web server that 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 when running the code below, there is no error:

#! /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 are some problems with my code. When I run the first perl script on my local machine using the -wc command, it says the syntax is fine. Help me please.

+3
source share
4 answers

Suppose you use the first script as a CGI script? You need to specify the type of content:

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

before any other exit (change text/plainto text/htmlor whatever, of course!)

+4
source

, script.

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

, CGI Programming script.

0

, , "500 Server Error", use CGI::Carp qw(FatalsToBrowser), - Perl - , perl /usr/bin/perl, cgi, - .

, 500, - -, , .

: , "" , "" . , , CGI ( ) , : 500 Internal Server Error "" ".

0

500

: CHMOD 755 /, .

0

Source: https://habr.com/ru/post/1746338/


All Articles