如何从 CGI Perl 脚本中的 POST 请求中获取查询字符串参数?

How to get query string parameters from POST request in CGI Perl script?

您可以使用以下代码在 CGI 脚本中获取查询参数:

#!C:\Perl64\bin\perl.exe

use CGI;

my $cgi = CGI->new();

my $file = $cgi->param( "file" );
print "Content-Type: text/plain\n\n";

my $text = do {
    open(my $f, "<:encoding(UTF-8)", $file)
      or die("Can't open $filename\": $!\n");
    local $/;
    <$f>
};

print $text;

但如果请求是 POST,这将不起作用,我正在使用 fetch API:

发送此请求
await fetch('cgi-bin/script.pl?file=..\foo.php', {
   method: 'POST',
   body: 'Some Text'
}).then(r => r.text());

如果我在浏览器中打开脚本,我会得到文件的内容。有没有办法告诉 CGI 模块从 QueryString 获取参数而不是 POST 数据?或任何其他在 Perl 中不使用 CGI 模块来解析查询字符串的原始方法?

用 URL 中的所有数据发出 POST 请求似乎很奇怪。为什么不做一个GET

但是,无论如何,CGI.pm 已经涵盖了您。文档中有一个名为 Mixing post and url parameters 的部分,内容如下:

my $color = url_param('color');

It is possible for a script to receive CGI parameters in the URL as well as in the fill-out form by creating a form that POSTs to a URL containing a query string (a "?" mark followed by arguments). The param() method will always return the contents of the POSTed fill-out form, ignoring the URL's query string. To retrieve URL parameters, call the url_param() method. Use it in the same way as param(). The main difference is that it allows you to read the parameters, but not set them.