vendion has asked for the wisdom of the Perl Monks concerning the following question:

I am working on a CGI script that the only form of input is by ether a POST or a PUT. The problem is no matter what I do in testing the result of CGI's param('POSTDATA') or param('PUTDATA') is always undefined. The way that I am accessing these variables are:

#!/usr/bin/perl use strict; use warnings; use CGI qw/ :standard -debug /; use CGI::Carp qw(fatalsToBrowser); print header; my $incoming = param('POSTDATA'); if ( ! defined $incoming ) { $incoming = param('PUTDATA'); } print $incoming;

The way I am testing this is using Curl to submit a XML file via a PUT to my CGI script, but when I run the script in offline mode I have the same issue. My curl command is as follows:

curl -T test.xml --trace output http://localhost/cgi-bin/api.pl

Replies are listed 'Best First'.
Re: Can't access {POST|PUT}DATA from CGI script
by chromatic (Archbishop) on Aug 16, 2011 at 00:40 UTC

    What's the form type of the POST/PUT data? As far as I understand CGI.pm, those pseudo-params are only valid when you don't have urlencoded or multipart data.

Re: Can't access {POST|PUT}DATA from CGI script
by derby (Abbot) on Aug 16, 2011 at 11:40 UTC

    chromatic is correct. From the 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.

    I'm not sure of all the curl command lines, but here's how I would test using LWP's lwp-request:

    lwp-request -m PUT -c 'text/xml' -se 'http://localhost/cgi-bin/api.pl +' < test.xml
    One caveat though, it cannot be 'application/xml' either. For some reasons XForms overreached and managed to grab that generic content-type and CGI does some special processing for it.

    -derby

    update: And to properly handle PUTDATA, you need your CGI version to be greater that 3.30. That support was actually hacked our here in the monastery: REST Webservices and CGI.pm.

      My CGI module 3.52, and the version on my production server is newer, I would say 3.55 because it was installed via CPAN last week. As for how curl works when given the -T flag it does a PUT and looks like this

      PUT /cgi-bin/api.pl?mode=MultiSpeak3 HTTP/1.1 User-Agent: curl/7.21.7 (x86_64-unknown-linux-gnu) libcurl/7.21.7 Open +SSL/1.0.0d zlib/1.2.5 libssh2/1.2.7 Host: localhost Accept: */* Content-Length: 6581 Expect: 100-continue (data)

      I placed a copy of the full PUT in my scratchpad includeing the data that I am trying to send. If I don't use the CGI module and grab from <STDIN> I am able to get the data back, doing it this way may make things harder on me in the long run which is why I am trying to use the CGI Module in the first place. I thought that maybe the CGI moudle was chocking on the newlines in the sample file that I have to work from but making a copy of it and stripping them out did not help.

      EDIT: Using your suggestion of LWP I was able to do a PUT to my script and it returned the data, so my problem is with CURL not giving a conetent type/CGI looking for a content type and not finding one or a little of both...

Re: Can't access {POST|PUT}DATA from CGI script
by Anonymous Monk on Aug 16, 2011 at 00:29 UTC

    no matter what I do in testing the result of CGI's param('POSTDATA') or param('PUTDATA') is always undefined.

    Do you have the latest version of CGI.pm?

    -debug is for commandline