我在CGI脚本中进行了以下验证,该脚本将检查GET方法并在未使用GET方法时返回405 HTTP状态代码。不幸的是,当使用POST或PUT时,它仍然会返回200状态OK。
my ($buffer);
# Read in text
$ENV{'REQUEST_METHOD'} =~ tr/a-z/A-Z/;
if ($ENV{'REQUEST_METHOD'} eq "GET")
{
$buffer = $ENV{'QUERY_STRING'};
}
else
{
print $cgi->header(
-type=>'text/plain',
-status=> '405 Method Not Allowed'
);
}
但是当我在客户端使用LWP并打印status_code时,它仍然提供200 OK,当我打印内容时,它正在打印状态:405
我在客户端使用的代码:
use LWP;
use HTTP:Request::Common;
my $ua = LWP::UserAgent->new;
my $req =PUT("/path/to/request");
my $result = $ua->request($req);
if($result->is_success) {
print "HTTP RESPONSE CODE:" , $result->status_line;
print $result->decoded_content;
}
从上面的代码中打印出有关成功的以下信息
HTTP RESPONSE CODE: 200 OK
Status: 405 Method Not Allowed
"some text regarding why method not allowed"
Requested Method Not allowed
如何将status_line与来自CGI标题的值匹配?
答案 0 :(得分:0)
根据此similar SO post,您可以尝试:
else
{
$cgi->$header->status('405 Method Not Allowed')
print $cgi->header('text/plain');
}
而不是你的:
else
{
print $cgi->header(
-type=>'text/plain',
-status=> '405 Method Not Allowed'
);
}