Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to catch value of parameter in Apache2 CGI

I have a little apache2 CGI application on the Ubuntu. The CGI handler is bash shell script.
My client application is search.html:

<html>
<body>
<form action="/cgi-bin/search.sh" method="post">
    <input type="text" name="searchKey" size="10"></input>
    <input type=SUBMIT value="search">
<form>
</body>
</html>

firstly, I just want to catch value of "searchKey" parameter in server side. I tried like following, but displaying nothing.
search.sh is:

#!/bin/bash
echo Content-type:text/plain 
echo ""

echo $SEARCHKEY

Guys, can you tell me how to catch value of the parameter in the server side?

UPDATE

thank you for all answers.I understood that to get a value of post request need to read data from STDIN.
i tried as Ithcy suggest like following

#!/bin/bash
echo post=$(</dev/stdin)
echo 'content length:'$CONTENT_LENGTH
echo 'content:'$post

it was displaying only that:

content length:30
content:

why is content nothing? do i need to do more configure on Apache server to read post data? Thanks

like image 810
Nyambaa Avatar asked Dec 30 '22 06:12

Nyambaa


2 Answers

POSTs will come through STDIN.

#!/bin/bash
POST=$(</dev/stdin)
echo $POST

But you really should look at using perl (or python, PHP, etc) if you can, as Glenn Jackman suggests.

like image 62
glomad Avatar answered Jan 05 '23 12:01

glomad


The whole querystring is represented in the $QUERY_STRING variable. You can see this by running env without arguments in your shell script.

Example for getting only the searchKey value:

echo $QUERY_STRING | sed 's/searchKey\=\([^&]\+\).*/\1/'

Update: I'm sorry, this only applies if you are using GET to post your form. I didn't read the details =/

If you really need to read POSTs, this page may help you: http://digitalmechanic.wordpress.com/2008/02/21/handling-post-data-in-bash-cgi-scripts/ I didn't get it to work, though.

like image 25
Emil Vikström Avatar answered Jan 05 '23 11:01

Emil Vikström