1

You can get query param in CGI script with this code:

#!C:\Perl64\bin\perl.exe

use CGI;

my $cgi = CGI->new();

my $file = $cgi->param( "file" );
print "Content-Type: text/plain\n\n";

my $text = do {
    open(my $f, "<:encoding(UTF-8)", $file)
      or die("Can't open \$filename\": $!\n");
    local $/;
    <$f>
};

print $text;

but this don't work if request is POST, I'm sending this request using fetch API:

await fetch('cgi-bin/script.pl?file=..\foo.php', {
   method: 'POST',
   body: 'Some Text'
}).then(r => r.text());

if I open the script in Browser I get content of the file. Is there a way to tell CGI module to get params from QueryString instead of POST data? or any other raw way to parse Query String without CGI module in Perl?

2

1 Answer 1

2

Seems weird to make a POST request with all of the data in the URL. Why not just make a GET?

But, anyway, CGI.pm has you covered. There's a section in the documentation called Mixing post and url parameters which says the following:

my $color = url_param('color');

It is possible for a script to receive CGI parameters in the URL as well as in the fill-out form by creating a form that POSTs to a URL containing a query string (a "?" mark followed by arguments). The param() method will always return the contents of the POSTed fill-out form, ignoring the URL's query string. To retrieve URL parameters, call the url_param() method. Use it in the same way as param(). The main difference is that it allows you to read the parameters, but not set them.

Sign up to request clarification or add additional context in comments.

3 Comments

Will this work without use CGI; my $cgi = CGI->new();? You didn't use $cgi->
@jcubic: Yeah, that works too. But I've never really seen the point of using CGI objects. Whoever needs more than one CGI object in a program?
@jcubic: Also, please read CGI::Alternatives and Please Don't Use CGI.pm.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.