1Plack::Request(3)     User Contributed Perl Documentation    Plack::Request(3)
2
3
4

NAME

6       Plack::Request - Portable HTTP request object from PSGI env hash
7

SYNOPSIS

9         use Plack::Request;
10
11         my $app_or_middleware = sub {
12             my $env = shift; # PSGI env
13
14             my $req = Plack::Request->new($env);
15
16             my $path_info = $req->path_info;
17             my $query     = $req->parameters->{query};
18
19             my $res = $req->new_response(200); # new Plack::Response
20             $res->finalize;
21         };
22

DESCRIPTION

24       Plack::Request provides a consistent API for request objects across web
25       server environments.
26

CAVEAT

28       Note that this module is intended to be used by Plack middleware
29       developers and web application framework developers rather than
30       application developers (end users).
31
32       Writing your web application directly using Plack::Request is certainly
33       possible but not recommended: it's like doing so with mod_perl's
34       Apache::Request: yet too low level.
35
36       If you're writing a web application, not a framework, then you're
37       encouraged to use one of the web application frameworks that support
38       PSGI (<http://plackperl.org/#frameworks>), or see modules like
39       HTTP::Engine to provide higher level Request and Response API on top of
40       PSGI.
41
42       If you're looking for an easy-to-use API to convert existing CGI
43       applications to run on PSGI, consider using CGI::PSGI or
44       CGI::Emulate::PSGI as well. CGI::Emulate::PSGI documentation has a good
45       summary of using them to convert existing CGI scripts to adapt to PSGI.
46

METHODS

48       Some of the methods defined in the earlier versions are deprecated in
49       version 0.99. Take a look at "INCOMPATIBILITIES".
50
51       Unless otherwise noted, all methods and attributes are read-only, and
52       passing values to the method like an accessor doesn't work like you
53       expect it to.
54
55   new
56           Plack::Request->new( $env );
57
58       Creates a new request object.
59

ATTRIBUTES

61       env Returns the shared PSGI environment hash reference. This is a
62           reference, so writing to this environment passes through during the
63           whole PSGI request/response cycle.
64
65       address
66           Returns the IP address of the client ("REMOTE_ADDR").
67
68       remote_host
69           Returns the remote host ("REMOTE_HOST") of the client. It may be
70           empty, in which case you have to get the IP address using "address"
71           method and resolve on your own.
72
73       method
74           Contains the request method ("GET", "POST", "HEAD", etc).
75
76       protocol
77           Returns the protocol (HTTP/1.0 or HTTP/1.1) used for the current
78           request.
79
80       request_uri
81           Returns the raw, undecoded request URI path. You probably do NOT
82           want to use this to dispatch requests.
83
84       path_info
85           Returns PATH_INFO in the environment. Use this to get the local
86           path for the requests.
87
88       path
89           Similar to "path_info" but returns "/" in case it is empty. In
90           other words, it returns the virtual path of the request URI after
91           "$req->base". See "DISPATCHING" for details.
92
93       query_string
94           Returns QUERY_STRING in the environment. This is the undecoded
95           query string in the request URI.
96
97       script_name
98           Returns SCRIPT_NAME in the environment. This is the absolute path
99           where your application is hosted.
100
101       scheme
102           Returns the scheme ("http" or "https") of the request.
103
104       secure
105           Returns true or false, indicating whether the connection is secure
106           (https).
107
108       body, input
109           Returns "psgi.input" handle.
110
111       session
112           Returns (optional) "psgix.session" hash. When it exists, you can
113           retrieve and store per-session data from and to this hash.
114
115       session_options
116           Returns (optional) "psgix.session.options" hash.
117
118       logger
119           Returns (optional) "psgix.logger" code reference. When it exists,
120           your application is supposed to send the log message to this
121           logger, using:
122
123             $req->logger->({ level => 'debug', message => "This is a debug message" });
124
125       cookies
126           Returns a reference to a hash containing the cookies. Values are
127           strings that are sent by clients and are URI decoded.
128
129           If there are multiple cookies with the same name in the request,
130           this method will ignore the duplicates and return only the first
131           value. If that causes issues for you, you may have to use modules
132           like CGI::Simple::Cookie to parse "$request->header('Cookie')" by
133           yourself.
134
135       query_parameters
136           Returns a reference to a hash containing query string (GET)
137           parameters. This hash reference is Hash::MultiValue object.
138
139       body_parameters
140           Returns a reference to a hash containing posted parameters in the
141           request body (POST). As with "query_parameters", the hash reference
142           is a Hash::MultiValue object.
143
144       parameters
145           Returns a Hash::MultiValue hash reference containing (merged) GET
146           and POST parameters.
147
148       content, raw_body
149           Returns the request content in an undecoded byte string for POST
150           requests.
151
152       uri Returns an URI object for the current request. The URI is
153           constructed using various environment values such as "SCRIPT_NAME",
154           "PATH_INFO", "QUERY_STRING", "HTTP_HOST", "SERVER_NAME" and
155           "SERVER_PORT".
156
157           Every time this method is called it returns a new, cloned URI
158           object.
159
160       base
161           Returns an URI object for the base path of current request. This is
162           like "uri" but only contains up to "SCRIPT_NAME" where your
163           application is hosted at.
164
165           Every time this method is called it returns a new, cloned URI
166           object.
167
168       user
169           Returns "REMOTE_USER" if it's set.
170
171       headers
172           Returns an HTTP::Headers::Fast object containing the headers for
173           the current request.
174
175       uploads
176           Returns a reference to a hash containing uploads. The hash
177           reference is a Hash::MultiValue object and values are
178           Plack::Request::Upload objects.
179
180       content_encoding
181           Shortcut to $req->headers->content_encoding.
182
183       content_length
184           Shortcut to $req->headers->content_length.
185
186       content_type
187           Shortcut to $req->headers->content_type.
188
189       header
190           Shortcut to $req->headers->header.
191
192       referer
193           Shortcut to $req->headers->referer.
194
195       user_agent
196           Shortcut to $req->headers->user_agent.
197
198       param
199           Returns GET and POST parameters with a CGI.pm-compatible param
200           method. This is an alternative method for accessing parameters in
201           $req->parameters just in case you want the compatibility with
202           CGI.pm objects.
203
204           You are not recommended to use this method since it is easy to
205           misuse in a list context such as inside a hash constructor or
206           method arguments. Use "parameters" and Hash::MultiValue instead.
207
208           Unlike CGI.pm, it does not allow setting or modifying query
209           parameters.
210
211               $value  = $req->param( 'foo' );
212               @values = $req->param( 'foo' );
213               @params = $req->param;
214
215       upload
216           A convenient method to access $req->uploads.
217
218               $upload  = $req->upload('field');
219               @uploads = $req->upload('field');
220               @fields  = $req->upload;
221
222               for my $upload ( $req->upload('field') ) {
223                   print $upload->filename;
224               }
225
226       new_response
227             my $res = $req->new_response;
228
229           Creates a new Plack::Response object. Handy to remove dependency on
230           Plack::Response in your code for easy subclassing and duck typing
231           in web application frameworks, as well as overriding Response
232           generation in middlewares.
233
234   Hash::MultiValue parameters
235       Parameters that can take one or multiple values (i.e. "parameters",
236       "query_parameters", "body_parameters" and "uploads") store the hash
237       reference as a Hash::MultiValue object. This means you can use the hash
238       reference as a plain hash where values are always scalars (NOT array
239       references), so you don't need to code ugly and unsafe "ref ... eq
240       'ARRAY'" anymore.
241
242       And if you explicitly want to get multiple values of the same key, you
243       can call the "get_all" method on it, such as:
244
245         my @foo = $req->query_parameters->get_all('foo');
246
247       You can also call "get_one" to always get one parameter independent of
248       the context (unlike "param"), and even call "mixed" (with
249       Hash::MultiValue 0.05 or later) to get the traditional hash reference,
250
251         my $params = $req->parameters->mixed;
252
253       where values are either a scalar or an array reference depending on
254       input, so it might be useful if you already have the code to deal with
255       that ugliness.
256
257   PARSING POST BODY and MULTIPLE OBJECTS
258       The methods to parse request body ("content", "body_parameters" and
259       "uploads") are carefully coded to save the parsed body in the
260       environment hash as well as in the temporary buffer, so you can call
261       them multiple times and create Plack::Request objects multiple times in
262       a request and they should work safely, and won't parse request body
263       more than twice for the efficiency.
264

DISPATCHING

266       If your application or framework wants to dispatch (or route) actions
267       based on request paths, be sure to use "$req->path_info" not
268       "$req->uri->path".
269
270       This is because "path_info" gives you the virtual path of the request,
271       regardless of how your application is mounted. If your application is
272       hosted with mod_perl or CGI scripts, or even multiplexed with tools
273       like Plack::App::URLMap, request's "path_info" always gives you the
274       action path.
275
276       Note that "path_info" might give you an empty string, in which case you
277       should assume that the path is "/".
278
279       You will also want to use "$req->base" as a base prefix when building
280       URLs in your templates or in redirections. It's a good idea for you to
281       subclass Plack::Request and define methods such as:
282
283         sub uri_for {
284             my($self, $path, $args) = @_;
285             my $uri = $self->base;
286             $uri->path($uri->path . $path);
287             $uri->query_form(@$args) if $args;
288             $uri;
289         }
290
291       So you can say:
292
293         my $link = $req->uri_for('/logout', [ signoff => 1 ]);
294
295       and if "$req->base" is "/app" you'll get the full URI for
296       "/app/logout?signoff=1".
297

INCOMPATIBILITIES

299       In version 0.99, many utility methods are removed or deprecated, and
300       most methods are made read-only. These methods were deleted in version
301       1.0001.
302
303       All parameter-related methods such as "parameters", "body_parameters",
304       "query_parameters" and "uploads" now contains Hash::MultiValue objects,
305       rather than scalar or an array reference depending on the user input
306       which is insecure. See Hash::MultiValue for more about this change.
307
308       "$req->path" method had a bug, where the code and the document was
309       mismatching. The document was suggesting it returns the sub request
310       path after "$req->base" but the code was always returning the absolute
311       URI path. The code is now updated to be an alias of "$req->path_info"
312       but returns "/" in case it's empty. If you need the older behavior,
313       just call "$req->uri->path" instead.
314
315       Cookie handling is simplified, and doesn't use CGI::Simple::Cookie
316       anymore, which means you CAN NOT set array reference or hash reference
317       as a cookie value and expect it be serialized. You're always required
318       to set string value, and encoding or decoding them is totally up to
319       your application or framework. Also, "cookies" hash reference now
320       returns strings for the cookies rather than CGI::Simple::Cookie
321       objects, which means you no longer have to write a wacky code such as:
322
323         $v = $req->cookies->{foo} ? $req->cookies->{foo}->value : undef;
324
325       and instead, simply do:
326
327         $v = $req->cookies->{foo};
328

AUTHORS

330       Tatsuhiko Miyagawa
331
332       Kazuhiro Osawa
333
334       Tokuhiro Matsuno
335

SEE ALSO

337       Plack::Response HTTP::Request, Catalyst::Request
338

LICENSE

340       This library is free software; you can redistribute it and/or modify it
341       under the same terms as Perl itself.
342
343
344
345perl v5.30.1                      2020-01-30                 Plack::Request(3)
Impressum