How to process and send POST requests in Perl and FastCGI?

Unfortunately, I am not familiar with Perl, so please here. I actually use FCGI with Perl.

I need 1. accept the POST request → 2. send it via POST to another URL → 3. get the results → 4. return the results to the first POST request (4 steps).

To accept the POST request (step 1), I use the following code (found it somewhere on the Internet):

$ENV{'REQUEST_METHOD'} =~ tr/a-z/A-Z/;
if ($ENV{'REQUEST_METHOD'} eq "POST") {
    read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
}
else {
    print ("some error");
}

@pairs = split(/&/, $buffer);
foreach $pair (@pairs) {
    ($name, $value) = split(/=/, $pair);
    $value =~ tr/+/ /;
    $value =~ s/%(..)/pack("C", hex($1))/eg;
    $FORM{$name} = $value;  
}

The content $name(this is a line) is the result of the first step. Now I need to send $namevia a POST request to some_url (step 2), which returns another result (step 3), which I should return as a result to the very first POST request (step 4).

Any help with this would be greatly appreciated.

Thank.

+3
2

POST, , CGI ( , Perl). POST LWP:: UserAgent

#/usr/bin/perl
use strict;
use warnings;
use CGI;
use LWP::UserAgent;

my $cgi = CGI->new;   # Will process post upon instantiation
my %params = $cgi->Vars;
my $ua = LWP::UserAgent->new;
my $postTo = 'http://www.somewhere.com/path/to/script';
my $response = $ua->post($postTo, %params);

if ($response->is_success) {
    print $response->decoded_content;  # or maybe $response->content in your case
} else {
 die $response->status_line;
}




}
+3

, , , . Perl - . . http://search.cpan.org/

CGI.pm -, ​​ Catalyst.

, , . , Perl .

+2

Source: https://habr.com/ru/post/1741689/


All Articles