Home > Mobile >  Perl CGI: mix URL and Body parameters
Perl CGI: mix URL and Body parameters

Time:11-24

I try to have a POST-request to a Perl CGI. All is fine if I have pure GET or pure POST.

my $q = CGI->new ();
my $method = $q->request_method ();
my $p1 = $q->param ("p1");
my $p2 = $q->param ("p2");

But when I mix URL-parameters with Body-parameters I do not get the URL-parameter with the POST-request. It is transferred correctly if I check what the browser sent.

<form method="post" action="http://localhost/cgi-bin/test/?p1=abc">
<input type="text" name="p2" value="xyz"/>
<input type="submit"/></br>
</form> 

Is it not possible to mix both?

CodePudding user response:

CGI has url_param for interrogating query parameters separately from POST fields.

This even provides some flexibility for deciding what-overrides-what in your code:

# URL parameter overrides POST field
my $p1 = $q->url_param('p1') // $q->param('p1');
# POST field overrides URL parameter
my $p2 = $q->param('p2') // $q->url_param('p2');

Note: // is a Perl operator for an "undef coalesce" introduced in v5.10. It's available even without use v5.10.

  • Related