Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

post

bash

shell

cgi

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).

like image 310
user2064000 Avatar asked Jun 12 '15 12:06

user2064000


People also ask

How does the posted data come to the CGI script?

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.

What is $@ in bash script?

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.

What does $_ mean in bash?

$_ (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. $@

What is $$ in shell script?

$$ The process number of the current shell. For shell scripts, this is the process ID under which they are executing.


1 Answers

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.

like image 52
Denis Avatar answered Sep 24 '22 00:09

Denis