如何在 Perl 中读取使用 POST 发送的 URL 数据?

发布于 2024-08-14 10:01:32 字数 429 浏览 4 评论 0原文

我正在尝试读出从页面中的表单发送到我的 Perl 脚本的 POST 数据。我用谷歌搜索并发现:

read(STDIN, $param_string, $ENV{'CONTENT_LENGTH'})

通过在正确的位置拆分它来读取整个数据字符串并将整个字符串写入 $param_string ,

Param1=Value1&Param2=Value2&Param3=Value3

我得到了必要的数据。

但我想知道为什么我的 $param_string 是空的。

当我用 GET 尝试整个事情时:

$param_string = $ENV{'QUERY_STRING'};

一切正常。有人有想法吗?

I'm trying to read out the POST-Data that was sent from a form in a page to my Perl Script. I googled and found out that:

read(STDIN, $param_string, $ENV{'CONTENT_LENGTH'})

reads out the whole Data-String with and writes the whole string to $param_string in the form of

Param1=Value1&Param2=Value2&Param3=Value3

by spliting it at the right places I get the necessary Data.

But I wonder why my $param_string is empty.

When I try the whole thing with GET:

$param_string = $ENV{'QUERY_STRING'};

everything works fine. Does anybody have an idea?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

嘿咻 2024-08-21 10:01:32

绝对没有真正的理由让像你这样级别的人想要手动解析 CGI 请求。

请使用 CGI::SimpleCGI.pm

CGI.pm 有很多包袱(HTML 生成、面向功能的界面),这使得 CGI::Simple 更好。

在 CPAN 上使用任何 CGI 处理模块都比尝试从头开始编写 CGI 处理代码要好。

请参阅 CGI::Simple 中的 parse_query_string 用于在处理 POST 到脚本的表单时访问使用查询字符串传递的参数的方法。

如果您想了解如何正确执行此操作,可以阅读任一模块的源代码。通读 CGI.pm CHANGES 文件也很有启发。

There absolutely no real reason for someone at your level to want to hand parse CGI requests.

Please use CGI::Simple or CGI.pm.

CGI.pm has a lot of baggage (HTML generation, function oriented interface) which makes CGI::Simple preferable.

Using any CGI processing module on CPAN is better than trying to write CGI processing code from scratch.

See parse_query_string in CGI::Simple for a way of accessing parameters passed using the query string when processing a form that is POSTed to your script.

If you want to learn how to do it right, you can read the source code of either module. Reading through the CGI.pm CHANGES file is also instructive.

纵山崖 2024-08-21 10:01:32

如果您能够检索 GET 数据但无法检索 POST 数据,很可能您忘记将表单方法从 post 更改为 post。您可以通过在 if 语句中使用此条件来检查您的提交方法:

if ($ENV{'REQUEST_METHOD'} eq "POST"){
   read(STDIN, $param_string, $ENV{'CONTENT_LENGTH'});
}else {
   $param_string = $ENV{'QUERY_STRING'};
}

If you are able to retrieve GET-data but not able to retrieve POST-data, most likely you forgot to change form method from to be post. You can check your submit method by using this condition in if statement:

if ($ENV{'REQUEST_METHOD'} eq "POST"){
   read(STDIN, $param_string, $ENV{'CONTENT_LENGTH'});
}else {
   $param_string = $ENV{'QUERY_STRING'};
}
枕梦 2024-08-21 10:01:32

在 mod_perl 2 下, Apache2::Request 对我有用。

Under mod_perl 2, Apache2::Request works for me.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文