3
votes

I have a third party Perl script designed to be executed by a browser and which processes data in a HTTP POST request.

The script can be executed from the command line but when doing so it is not being executed in the expected environment and as such does not have access to required user data.

I need to execute the script very frequently and want to avoid the overhead of spinning up an Apache process to handle what can instead be handled at the command line. The script executes from the command line much much much faster than via Apache (at least with no input data).

I'd like to wrap up the script such that command line arguments are passed to the script as if they were present in a HTTP POST request.

I'm not very familiar with Perl and would like to present a (rudimentary) example in PHP to represent what I intend to achieve.

<?php

$_POST['example1'] = $argv[1];
$_POST['example2'] = $argv[2];

include /var/www/thirdPartyScript.php

?>

The Perl script accesses data as follows:

#!/usr/bin/perl -T

use CGI 3.40 qw(-newstyle_urls -private_tempfiles redirect);
# ...
my $q = CGI->new();
# ...
if ($q->param('example1') {

} else {

}


What would the wrapper script need to set such that the third party script has access to relevant data as if that data were provided in a HTTP POST request?

2

2 Answers

5
votes

If the third party script is CGI, you can provide the parameters on the command line:

your_script.pl name1=value1&name2=value2

See more details on how to test the POST and GET methods in the CGI.pm documentation.

2
votes

You can easily make a real HTTP request for testing purposes:

use LWP::UserAgent qw();
my $ua = LWP::UserAgent->new;
$ua->post(
    'http://localhost:5000',
    [
        foo => 23,
        bar => 42,
    ]
);

You can initialise the CGI object from a hashref or a file.

use CGI qw();
my $c = CGI->new({
    foo => 23,
    bar => 42,
});

Prefer real HTTP requests over these fake ones! The fake ones, including the command-line variant in January's answer, lack some properties, e.g. request_method.