I'm writing a web application using CGI scripts written in bash.
For GET
requests, 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 POST
requests.
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 POST
requests?
(If it matters, I'm using Apache 2.4).
POST sends the data through standard input, while GET passes the information through environment variables. If no method is specified, the server defaults to GET.
bash [filename] runs the commands saved in a file. $@ refers to all of a shell script's command-line arguments. $1 , $2 , etc., refer to the first command-line argument, the second command-line argument, etc. Place variables in quotes if the values might have spaces in them.
$_ (dollar underscore) is another special bash parameter and used to reference the absolute file name of the shell or bash script which is being executed as specified in the argument list. This bash parameter is also used to hold the name of mail file while checking emails. $@
$$ The process number of the current shell. For shell scripts, this is the process ID under which they are executing.
When using the POST method. The POST values will be the input to your CGI program. So in bash just use
read POST_STRING
POST_STRING then contains the POST values in the same format as QUERY_STRING holds the values for a GET request.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With