Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

CGI - How to use upload_hook and query the query string ?

by roadrunner (Acolyte)
on Feb 07, 2006 at 17:57 UTC ( #528578=perlquestion: print w/replies, xml ) Need Help??
roadrunner has asked for the wisdom of the Perl Monks concerning the following question:


More upload-related questioning - I'm using the CGI::upload_hook functionality and would like to get a parameter value passed in via POST *before* calling the upload hook to process files. i.e. Something like this:

my $query = new CGI();<br/> $query->param("myParam");<br/> $query->new(\&hook);

Now according to the CGI docs this doesn't look possible:

"If using the function-oriented interface, call the CGI::upload_hook() method before calling param() or any other CGI functions: CGI::upload_hook(\&hook,$data);"

...and indeed my exprience bears this out - If I try and query any params before calling the upload hook I get missing data in the hook. Does anyone know of a way round this - i.e. querying parameter values before calling an upload hook ?


Replies are listed 'Best First'.
Re: CGI - How to use upload_hook and query the query string ?
by cees (Curate) on Feb 07, 2006 at 18:06 UTC

    If you think about this for a while, then you will realize that it is impossible for you to access any POST parameters before you use the upload hook functionality. The uploaded files are actually part of the POST parameters, so in order to access the POST parameters, you would have to parse the entire POST request (includng the files). After that, what is the point of using the upload hook, since the file is already uploaded and in a temporary file on the server.

    The way I got around it is to put the parameters in the URL action of the form as simple query paramaters. Those are accessible to you at any time in the $ENV{QUERY_STRING} variable, and you can fairly easily parse those manually and pull out your parameters before you invoke to parse the POST request.

      No need to parse it manually
      my $param = do { local $ENV{'REQUEST_METHOD'} = 'GET'; local $ENV{'CONTENT_LENGTH'}; local @QUERY_PARAM; my $cgi = CGI->new(); $cgi->param(...) }; my $cgi; if ($param) { $cgi = CGI->new(\&hook); } else { $cgi = CGI->new(); }

      Untested, even less so with mod_perl.

      Update: Added local @QUERY_PARAM;.

Re: CGI - How to use upload_hook and query the query string ?
by ikegami (Pope) on Feb 07, 2006 at 18:22 UTC

    The hook needs to be called when the request is being parsed (i.e. in new), and the request needs to be parsed in order to know what parameters were supplied. .

    Fortunately, the QUERY_STRING is parsed before the body is parsed, so you can use param to check parameters from the QUERY_STRING inside of your hook. Unforunately, the CGI request is not passed to the hook, so you'll have to store the CGI request in a "global":

    my $cgi; sub hook { return if not $cgi->param(...); my (...) = @_; ... } $cgi = CGI->new(\&hook);

      I thought that by default did not parse the QUERY_STRING if it was a POST request... You actually have to edit the code to get it to parse and combine both the GET and POST parameters together.

      Search the source for for the string "Some people want to have their cake and eat it too".

        Your right. I looked at the source, but I looked too quickly and mistook something else for the parsing of QUERY_STRING. They'll have to modify their or use my alternate solution.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://528578]
Approved by idsfa
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (4)
As of 2018-06-19 09:39 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (112 votes). Check out past polls.