Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
328 views
in Technique[技术] by (71.8m points)

perl - Are there any problems handling a POST request as a GET request on the server

In an attempt to resolve the issue I posted in this question:

Is it possible to send POST parameters to a CGI script using a system() call?

So far the only successful resolution to the problem is to trick the environment to think the request was a GET. I do this by converting the POST parameters to a query string, saving that string in the default environment variable, then changing the environment variable that tells the server what method this request is using to GET.

$ENV{'QUERY_STRING'} = $long_parameter_string . '&' . $ENV{'QUERY_STRING'};
$ENV{'REQUEST_METHOD'} = 'GET';

system {$perl_exec} $cgi_script;

I'm essentially tricking the CGI module to read from the QUERY_STRING environment variable instead of from STDIN, which it would attempt to read POST requests from.

This method seems to work so far, but I'm worried about unintended repercussions.

My question is, do you see any potential problems with this?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

POST and GET mean entirely different things, and you shouldn't be "testing" anything that way.

Instead, you should do a real POST to the intended URL by using Perl's LWP.

use HTTP::Request::Common qw(POST);
use LWP::UserAgent;
$ua = LWP::UserAgent->new;

my $req = POST 'http://www.perl.com/cgi-bin/BugGlimpse',
              [ param1 => 'arbitrarily long blob', param2 => 'whatever' ];

print $ua->request($req)->as_string;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...