Perl CGI with HTTP Status Codes

坚强是说给别人听的谎言 提交于 2019-12-04 03:51:25

问题


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'
);


来源:https://stackoverflow.com/questions/19872574/perl-cgi-with-http-status-codes

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!