具有HTTP状态代码的Perl CGI [英] Perl CGI with HTTP Status Codes
问题描述
我在CGI脚本中进行了以下验证,该脚本将检查GET方法并在未使用GET方法时返回405 HTTP状态代码。不幸的是,当使用POST或PUT时它仍然返回200状态OK。
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');
}
我还是CGI编程的新手,所以我想这里有人可以折腾我关于使用CGI和HTTP状态返回的主题。如果提供了一个非常好的CGI文档,因为大多数搜索返回的是CPAN(已经读过几次)和非面向对象的旧教程。
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.
推荐答案
cpan docs不仅仅是 enought 为CGI。如果你想要新教程不使用CGI,请使用MVC框架之一(Catalyst,Dancer2,Mojo等)。
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 ).
如果要更改,你可以发布405标题:
You can post 405 header if will change:
$cgi->$header->status('405 Method Not Allowed');
print $cgi->header('text/plain');
到此:
print $cgi->header(
-type=>'text/plain',
-status=> '405 Method Not Allowed'
);
这篇关于具有HTTP状态代码的Perl CGI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!