GL2014 GL2014 - 1 year ago 54
Perl Question

How to get URLencoded data from the body of a POST in CGI Perl

POSTDATA is not the correct answer. I have read the docs and still don't see how I can get the data.

I want to receive this request:

POST /cgi-bin/myscript.cgi HTTP/1.1
Content-Length: 3
Content-Type: application/x-www-form-urlencoded


and have the server respond
You sent the string "255"

Please assist, I am a Perl beginner and have gotten a bunch of seemingly wrong and useless answers to this seemingly simple request.

Answer Source

CGI will automatically parse form data, so you need to hide that what you got is form data (or at least claims to be).

use CGI qw( );
$ENV{CONTENT_TYPE} = 'application/octet-stream';
my $cgi = CGI->new();
my $post_data = $cgi->param('POSTDATA');

Better solution: Have the requester use a correct content type (e.g. application/octet-stream), or have the requester actually send form data (e.g. data=255).