0
votes

I need a simple CGI based Perl script to receive a POST (directly, not from another HTML page) with Content-Type being application/x-www-form-urlencoded and to echo back

I received: (encoded string)

(and if possible) decoded, the string is: (decoded string)

I am new to CGI Perl, and this is a one off request for testing a product (I'm a sysadmin. not a programmer). I intend to learn Perl more deeply in the future, but in this case I'm hoping for a gimme.

1
perldoc.perl.org/CGI.html#SYNOPSIS is a good start for CGI.mpapec
"to receive a POST (directly, not from another HTML page)" — A POST request is a POST request. The server cares nothing about what caused the client to generate it unless your write code to explicitly examine the referrer (or you use something like an anti-CSRF nonce)Quentin

1 Answers

0
votes

To start off, I will quickly skim some of the basics.

Following is the package for PERL/CGI application:

use CGI;

To create CGI object:

my $web = CGI->new;

Make sure you set and then write HTTP headers to outstream, before flushing out any CGI data to outstream. Otherwise you would end up in 500 error.

To set the headers:

print $web->header();
print $web->header('application/x-www-form-urlencoded');

To receive any post data from HTML, say for example,

http://example.com?POSTDATA=helloworld

you may use param() function:

my $data = $web->param('POSTDATA');

scalar $data would be set with "helloworld".

It is advisable to check if $web->param('POSTDATA') is defined before you assign to a scalar.