Perl CGI with HTTP Status Codes

2020-04-07 04:40发布

问题:

I have the following validation in a CGI script that will check for the GET method and return a 405 HTTP status code if the GET method is not used. Unfortunately it is still returning a 200 Status OK when using POST or PUT.

my ($buffer);
# Read in text
$ENV{'REQUEST_METHOD'} =~ tr/a-z/A-Z/;
if ($ENV{'REQUEST_METHOD'} eq "GET")
{
    $buffer = $ENV{'QUERY_STRING'};
}
else
{
    $cgi->$header->status('405 Method Not Allowed')
    print $cgi->header('text/plain');
}

I am still new to CGI programming so I figured someone here could toss me a bone about working with CGI and HTTP status returns. If a good CGI doc is provided that would be awesome, as most returned by search are CPAN (already read a few times) and really old tutorials that are not Object oriented.

回答1:

cpan docs is more than enought for CGI. If you want new tutorials don't use CGI, use one of MVC frameworks ( Catalyst, Dancer2, Mojo, etc ).

You can post 405 header if will change:

$cgi->$header->status('405 Method Not Allowed');
print $cgi->header('text/plain');

to this:

print $cgi->header(
   -type=>'text/plain',
   -status=> '405 Method Not Allowed'
);


标签: perl http cgi w3c