Apache2::Request(3pm)
π NAME π
Apache2::Request β methods for dealing with client request data β¨
okay bestie, so real talk: this is the Apache2::Request(3pm) page, straight out of the User Contributed Perl Documentation section, and it's literally your whole guide to grabbing stuff out of incoming HTTP requests. GET params, POST params, uploads, all of it. Let's go π
β¨ SYNOPSIS β¨
use Apache2::Request;
$req = Apache2::Request->new($r);
@foo = $req->param("foo");
$bar = $req->args("bar");
see? not scary. four lines and you're already parsing π«Ά
πΈ DESCRIPTION πΈ
So the Apache2::Request module gives you methods for parsing GET and POST parameters that got encoded with either application/x-www-form-urlencoded or multipart/form-data. And yes, Apache2::Request does hand you a few shiny new APIs for poking at the parsed data, but it stays largely backwards-compatible with the original 1.X API. π careful tho β if you're porting, go read the "PORTING from 1.X" section below, it has the whole list of known issues.
This manpage documents the Apache2::Request package. periodt.
Apache2::Request π
The interface is designed to mimic the CGI.pm routines for parsing query parameters, bestie. iykyk. But there are main differences:
Apache2::Request::newtakes an environment-specific object $r as (second) argument. Newer versions ofCGI.pmalso accept this syntax within modperl.- The query parameters are stored in
APR::Tablederived objects, and are therefore retrieved from the table by using case-insensitive keys. lowercase girlies win again π - The query string is always parsed immediately, even for POST requests. no waiting, no cap.
new π
Apache2::Request->new($r, %args)
Creates a new Apache2::Request object. iconic behaviour.
my $req = Apache2::Request->new($r, POST_MAX => "1M");
With mod_perl2, the environment object $r must be an Apache2::RequestRec object. In that case, all methods from Apache2::RequestRec are inherited. In the (default) CGI environment, $r must be an APR::Pool object.
The following args are optional (yes, all of them, go wild):
POST_MAX,MAX_BODYβ limit the size of POST data (in bytes).DISABLE_UPLOADSβ disable file uploads. π«TEMP_DIRβ sets the directory where upload files are spooled. On a *nix-like that supportslink(2), theTEMP_DIRshould be located on the same file system as the final destination file:
use Apache2::Upload;
my $req = Apache2::Request->new($r, TEMP_DIR => "/home/httpd/tmp");
my $upload = $req->upload('file');
$upload->link("/home/user/myfile");
For more details on link, see Apache2::Upload.
HOOK_DATAβ extra configuration info passed as the fourth argument to an upload hook. See the description for the next item,UPLOAD_HOOK.UPLOAD_HOOKβ sets up a callback to run whenever file upload data is read. This can be used to provide an upload progress meter during file uploads. π Apache will automatically continue writing the original data to$upload->fhafter the hook exits.
my $transparent_hook = sub {
my ($upload, $data, $data_len, $hook_data) = @_;
warn "$hook_data: got $data_len bytes for " . $upload->name;
};
my $req = Apache2::Request->new($r,
HOOK_DATA => "Note",
UPLOAD_HOOK => $transparent_hook,
);
instance π¦
Apache2::Request->instance($r)
The default (and only) behavior of Apache2::Request is to intelligently cache POST data for the duration of the request. So there's no longer any need for a separate instance() method like the one that existed in Apache2::Request for Apache 1.3 β all POST data is always available from each and every Apache2::Request object created during the request's lifetime. every single one. iconic.
However, an instance() method is aliased to new() in this release, to ease the pain of porting from 1.X to 2.X. we love a soft landing π«Ά
param π
$req->param()
$req->param($name)
Get the request parameters (using case-insensitive keys) by mimicking the OO interface of CGI::param.
# similar to CGI.pm
my $foo_value = $req->param('foo');
my @foo_values = $req->param('foo');
my @param_names = $req->param;
# the following differ slightly from CGI.pm
# returns ref to APR::Request::Param::Table object representing
# all (args + body) params
my $table = $req->param;
@table_keys = keys %$table;
In list context, or when invoked with no arguments as $req->param(), param induces libapreq2 to read and parse all remaining data in the request body. However, scalar $req->param("foo") is lazy β libapreq2 will only read and parse more data if
- no
fooparam appears in the query string arguments, AND - no
fooparam appears in the previously parsed POST data.
In this circumstance libapreq2 will read and parse additional blocks of the incoming request body until either
- it has found the the
fooparam, or - parsing is completed.
Observe that scalar $req->param("foo") will not raise an exception if it can locate foo in the existing body or args tables, even if the query-string parser or the body parser has failed. In all other circumstances param will throw an Apache2::Request::Error object into $@ should either parser fail. π
$req->args_status(1); # set error state for query-string parser
ok $req->param_status == 1;
$foo = $req->param("foo");
ok $foo == 1;
eval { @foo = $req->param("foo") };
ok $@->isa("Apache2::Request::Error");
undef $@;
eval { my $not_found = $req->param("non-existent-param") };
ok $@->isa("Apache2::Request::Error");
$req->args_status(0); # reset query-string parser state to "success"
Note: modifications to thescalar $req->param()table only affect the returned table object (the underlying Capr_table_tis generated from the parse data byapreq_params()). Modifications do not affect the actual request data, and will not be seen by other libapreq2 applications. π
parms, params π
The functionality of these functions is assumed by param, so they're no longer necessary. Aliases to param are provided in this release for back-compat, however they are deprecated and may be removed from a future release. so like⦠use param, bestie.
body π
$req->body()
$req->body($name)
Returns an APR::Request::Param::Table object containing the POST data parameters of the Apache2::Request object.
my $body = $req->body;
An optional name parameter can be passed to return the POST data parameter associated with the given name:
my $foo_body = $req->body("foo");
More generally, body() follows the same pattern as param() with respect to its return values and argument list. The main difference is that modifications to the scalar $req->body() table affect the underlying apr_table_t attribute in apreq_request_t, so their impact will be noticed by all libapreq2 applications during this request. yeah. everybody sees it. no secrets. β¨
upload π
$req->upload()
$req->upload($name)
Requires Apache2::Upload. With no arguments, this method returns an APR::Request::Param::Table object in scalar context, or the names of all Apache2::Upload objects in list context.
An optional name parameter can be passed to return the Apache2::Upload object associated with the given name:
my $upload = $req->upload($name);
More generally, upload() follows the same pattern as param() with respect to its return values and argument list. The main difference is that its returned values are Apache2::Upload object refs, not simple scalars. glow-up fr fr.
Note: modifications to thescalar $req->upload()table only affect the returned table object (the underlying Capr_table_tis generated byapreq_uploads()). They do not affect the actual request data, and will not be seen by other libapreq2 applications. π
args_status π
$req->args_status()
Get the APR status code of the query-string parser. APR_SUCCESS on success, error otherwise.
body_status πΈ
$req->body_status()
Get the current APR status code of the parsed POST data. APR_SUCCESS when parser has completed, APR_INCOMPLETE if parser has more data to parse, APR_EINIT if no post data has been parsed, error otherwise.
param_status β
$req->param_status()
In scalar context, this returns args_status if there was an error during the query-string parse, otherwise this returns body_status, ie
$req->args_status || $req->body_status
In list context param_status returns the list (args_status, body_status).
parse π₯
$req->parse()
Forces the request to be parsed immediately. In void context, this will throw an APR::Request::Error should either the query-string or body parser fail. In all other contexts it will return the two parsers' combined APR status code
$req->body_status || $req->args_status
However parse should be avoided in most normal situations. For example, in a mod_perl content handler it is more efficient to write
sub handler {
my $r = shift;
my $req = Apache2::Request->new($r);
$r->discard_request_body; # efficiently parses the request body
my $parser_status = $req->body_status;
#...
}
Calling $r->discard_request_body outside the content handler is generally a mistake, so use $req->parse there, but only as a last resort. The Apache2::Request API is designed around a lazy-parsing scheme, so calling parse should not affect the behavior of any other methods. π§
π¦ SUBCLASSING Apache2::Request π¦
If the instances of your subclass are hash references then you can actually inherit from Apache2::Request as long as the Apache2::Request object is stored in an attribute called r or _r. (The Apache2::Request class effectively does the delegation for you automagically, as long as it knows where to find the Apache2::Request object to delegate to.) main character energy tbh. For example:
package MySubClass;
use Apache2::Request;
our @ISA = qw(Apache2::Request);
sub new {
my($class, @args) = @_;
return bless { r => Apache2::Request->new(@args) }, $class;
}
and that's where part 1 wraps, bestie β more coming in part 2 π don't go anywhere π«Ά
π PORTING from 1.X π
okay bestie, this is the whole entire list of changes to methods that already existed back in Apache2::Request 1.X. If you're moving a 1.X app over to the new 2.X API, these are the things you gotta handle. No skipping, I see you. π
- Apache2::Upload is now a separate module. Any app that needs the upload API has to
use Apache2::Uploadin 2.X. Good news: this one's easy-peasy to fix, just preload the modules during server startup. β¨ - You can no longer add (or set or delete) parameters in the
scalar $req->param,scalar $req->argsorscalar $req->bodytables. And you can't add (or set or delete) cookies in thescalar $req->jartable either. She's locked, bestie. π (not in a cute way) instance()is now identical tonew(), and it's officially deprecated. It may get removed from a future 2.X release. So likeβ¦ usenew(), iykyk. πparamnow includes the functionality ofparms()andparams(), which means those two are now deprecated too and may be removed from a future 2.X release. They're giving "retired but still hanging around the group chat." πparamcalled in a list context no longer returns a unique list of parameters. The returned list contains multiple instances of the parameter name for multivalued fields. Not a bug, that's just the vibe now. π¦
for real though, if your code was leaning on the old table-mutation behaviour or the de-duped list return, that's the stuff that breaks. go check it, bestie. π«Ά
π SEE ALSO π
Go read these next, they're the besties of this page:
APR::Request::ParamAPR::Request::ErrorApache2::UploadApache2::CookieAPR::Table(3)
π COPYRIGHT π
here's the legal bit, and I'm keeping it character for character because that's how licenses work β they don't do "vibes-based paraphrasing":
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
and that's a wrap on Apache2::Request(3pm) π this was perl v5.42.3, dated 2026-08-21, and honestly? she served. go read the whole thing, then go write some beautiful Perl. slay, you got this πβ¨π¦π
π¦ where this page comes from
- ubuntu stonking: libapreq2-doc 2.17-9build1 Β· Apache-2.0
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms