4

In PHP, php://input enables to read the raw request body data, which is what I need in this case. However the rest of the server-side backbone is all written in PERL, so I tried to find the alternative to php://input -- I couldn't.

Short question:

How can one get the raw POST data (request body) in Perl? (the CGI variable showed absolutely nothing).

Thanks!

Chris
  • 26,544
  • 5
  • 58
  • 71
  • 2
    is this post what you need? http://stackoverflow.com/questions/908113/how-can-i-get-the-entire-request-body-with-cgi-pm – fersarr Dec 10 '12 at 07:38

1 Answers1

7

From the CGI module's docs,

If POSTed data is not of type application/x-www-form-urlencoded or multipart/form-data, then the POSTed data will not be processed, but instead be returned as-is in a parameter named POSTDATA. To retrieve it, use code like this:

my $data = $query->param('POSTDATA');
Community
  • 1
  • 1
ikegami
  • 367,544
  • 15
  • 269
  • 518
  • Yep, that should do it. Thanks a lot! – Chris Dec 10 '12 at 07:42
  • Well, I have a tiny question: is this binary-safe? I'm POSTing an array buffer from JavaScript, and I plan to write it to a file from PERL (it's raw PCM audio samples). – Chris Dec 10 '12 at 07:43
  • 1
    @Abody97: It should be. However if you're on Windows, you have to take care to open the file in binary mode. – reinierpost Dec 10 '12 at 14:14
  • `binmode` can be necessary on all systems. Just do `binmode($fh)` after you open the output file. – ikegami Dec 10 '12 at 16:26
  • @ikegami - Your answer leads me to another 2 questions: **(1)** What if one of the parameters has name `POSTDATA` - how CGI handles such case? **(2)** How can we get raw POST data for Content-Type of *multipart/form-data* to be able find boundaries and read binary files such as PDF? – Ωmega Oct 07 '19 at 12:32
  • @Ωmega, 1) I don't know. 2) CGI.pm handles form-data just as well as x-www-form-urlencoded. File fields are returned as a file handle. Search the docs for "upload" – ikegami Oct 07 '19 at 16:37
  • @ikegami - Why not to read **raw** data from STDIN before `CGI` does? *(see my answer below)* – Ωmega Oct 07 '19 at 16:52
  • Don't you have to place it in a BEGIN block before `use CGI;` (so it gets executed before CGI.pm reads it), or am I remembering wrong? Anyway, just it's just a whole lot more complicated than `my $data = $query->param('POSTDATA');`. – ikegami Oct 07 '19 at 16:55