-2

POSTDATA 不是正确的答案。我已经阅读了文档,但仍然看不到如何获取数据。

我想收到这个请求:

POST /cgi-bin/myscript.cgi HTTP/1.1
Host: myhost.com
Content-Length: 3
Content-Type: application/x-www-form-urlencoded

255

并让服务器响应您发送了字符串“255”

请帮忙,我是一个 Perl 初学者,对于这个看似简单的请求,我得到了一堆看似错误和无用的答案。

4

3 回答 3

1

CGI 将自动解析表单数据,因此您需要隐藏您获得的是表单数据(或至少声称是)。

use CGI qw( );
$ENV{CONTENT_TYPE} = 'application/octet-stream';
my $cgi = CGI->new();
my $post_data = $cgi->param('POSTDATA');

更好的解决方案:让请求者使用正确的内容类型(例如application/octet-stream),或者让请求者实际发送表单数据(例如data=255)。

于 2013-05-30T21:42:12.160 回答
0

对我来说独特的解决方案是将客户请愿书上的 ContentType 更改为“application/octet-stream”

模块 CGI CPAN 说:

如果 POST 数据的类型不是application/x-www-form-urlencodedmultipart/form-data,则不会处理 POST 数据,而是在名为 POSTDATA 的参数中按原样返回。

因此,如果您无法将客户请求更改为其他 ContentType,则不会对其进行处理。

于 2015-06-02T13:25:50.997 回答
-1

CGI(至少在最近的版本中)会将错误编码x-www-form-urlencoded的参数填充到名为keywords. 最好发送正确的内容类型,然后 POSTDATA 就像文档所说的那样工作:

如果 POST 数据的类型不是 application/x-www-form-urlencoded 或 multipart/form-data,那么 POST 数据将不会被处理...

use strictures;
use CGI::Emulate::PSGI;
use Plack::Test;
use HTTP::Request::Common;
use Test::More;

my $post = POST "/non-e-importa",
    "Content-Length" => 5,
    "Content-Type" => "application/x-www-form-urlencoded",
    Content => "ohai\n";

my $cgis = CGI::Emulate::PSGI->handler( sub {
    use CGI "param", "header";
    my $incorrectly_encoded_body = param("keywords");
    print header("text/plain"), $incorrectly_encoded_body;
});

test_psgi $cgis, sub {
    my $cb  = shift;
    my $res = $cb->($post);
    is $res->content, "ohai", "Soopersek437 param: keywords";
};

done_testing();

__END__
prove so-16846138 -v
ok 1 - Soopersek437 param: keywords
1..1
ok
All tests successful.
Result: PASS
于 2013-05-30T21:59:49.840 回答