如何从用 bash 编写的 CGI 脚本中获取 POST 参数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30802775/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 13:11:52  来源:igfitidea点击:

How to get POST parameters from CGI scripts written in bash?

bashshellpostcgi

提问by user2064000

I'm writing a web application using CGI scripts written in bash.

我正在使用用 bash 编写的 CGI 脚本编写 Web 应用程序。

For GETrequests, the request parameters are available in a variable named $QUERY_STRING. However, I'm unable to figure out where the similar value would be stored for POSTrequests.

对于GET请求,请求参数在名为 的变量中可用$QUERY_STRING。但是,我无法弄清楚为POST请求存储类似值的位置。

I'm using the following script:

我正在使用以下脚本:

#!"c:/msys64/usr/bin/bash.exe"
# On *nix, replace above with #!/bin/bash

echo -en "Status: 200 OK\r\n"
echo -en "Content-type: text/plain\r\n\r\n"

declare -x
declare -a

And this is what I get:

这就是我得到的:

$ curl -so - --data "abc=ZZZZZZ&d=PPPPPPPP" http://localhost/cgi-bin/test.sh | grep ZZZZZZ

$ curl -so - "http://localhost/cgi-bin/test.sh?abc=ZZZZZZ&d=PPPPPPPP" | grep ZZZZZZ
declare -x QUERY_STRING="abc=ZZZZZZ&d=PPPPPPPP"
declare -x REQUEST_URI="/cgi-bin/test.sh?abc=ZZZZZZ&d=PPPPPPPP"

How can I retrieve the values sent over POSTrequests?

如何检索通过POST请求发送的值?

(If it matters, I'm using Apache 2.4).

(如果重要的话,我使用的是 Apache 2.4)。

采纳答案by Denis

When using the POST method. The POST values will be the input to your CGI program. So in bash just use

使用 POST 方法时。POST 值将作为 CGI 程序的输入。所以在 bash 中只需使用

read POST_STRING

POST_STRING then contains the POST values in the same format as QUERY_STRING holds the values for a GET request.

POST_STRING 然后包含与 QUERY_STRING 保存 GET 请求的值相同格式的 POST 值。