How to handle POST request to PERL from html

2019-04-18 02:19发布

问题:

I am writing a small cPanel plugin which basically submit an html form to a perl file over ajax request to the perl file.

The type of the request is POST as I need to send some data to the script.

My Question is how can I handle the POST data in perl?

Now, I have found few implementations with the CGI library, however I do not have that available as default cPanel perl module. Furthermore I have found few examples with the LWP library, however I am failing to understand those. Can someone give me any suggestion on this.

Thank you!

回答1:

use CGI qw( );
my $cgi = CGI->new();
my $json = $cgi->param('POSTDATA');


回答2:

All that you need to know is in RFC3875 — the specification for The Common Gateway Interface Version 1.1

Essentially, the CGI program is passed information about the incoming request message through both the environment variables and the STDIN input channel. If the CGI program is a Perl one then the environment variables can be accessed through the built-in hash %ENV, which will contain a fixed set of values such as REQUEST_METHOD (which will normally be HEAD, GET, or POST) or CONTENT_LENGTH

The body of the message is delivered to the CGI program via its STDIN channel, and the value of CONTENT_LENGTH indicates how many bytes should be read from the channel to fetch the complete message body

In the case of a POST message that represents form input from the browser, REQUEST_METHOD will, obviouly, be POST; CONTENT_TYPE will be application/x-www-form-urlencoded (unless one of the form fields is a file upload); and CONTENT_LENGTH will hold the number of bytes of data in the request message body, which is the amount of data that the CGI program should read from STDIN. This should be done using read STDIN instead of the usual <STDIN> because the server may not put an EOF after the valid body data. Your code will look like

my $bytes_read = read STDIN, my $form_data, $ENV{CONTENT_LENGTH}

after which the form data will be saved in $form_data in the same format as the query portion of a GET URL (that's what the urlencoded part of the content type means)

If you need to know any more about what the CGI environment provides and expects then take a look at the RFC that I have linked above. It's fairly straightforward to read



回答3:

Borodin's answer says all. Basically, you don't need the CGI.pm overhead:

#!/usr/bin/perl -T
use strict;
use warnings;

my %form;
if ($ENV{REQUEST_METHOD} eq 'POST') {
    read(STDIN, my $form_data, $ENV{CONTENT_LENGTH})
      or die "$!\n";

    # URL decode
    chomp($form_data);
    $form_data =~ tr/+/ /;
    $form_data =~ s/%([a-f0-9][a-f0-9])/chr(hex($1))/egi;

    foreach (split /&/, $form_data) {
        # trim spaces
        s/^\s+|\s+$//g;

        # get the URL param
        my($key, $value) = split(/=/, $_, 2);
        $form{$key} = $value || '';
    }
}

a dichotomy

HTML <input type="text" name="email" />

CGI my $email = $form{email};



标签: perl post cpanel