Dancer::Request - interface for accessing incoming requests
This class implements a common interface for accessing incoming requests in a Dancer application.
In a route handler, the current request object can be accessed by the request method, like in the following example:
request
get '/foo' => sub { request->params; # request, params parsed as a hash ref request->body; # returns the request body, unparsed request->path; # the path requested by the client # ... };
A route handler should not read the environment by itself, but should instead use the current request object.
Return the HTTP method used by the client to access the application.
Return the path requested by the client.
Returns an absolute URI for the base of the application
Constructs a URI from the base and the passed path. If params (hashref) is supplied, these are added to the query string of the uri. If the base is http://localhost:5000/foo, request->uri_for('/bar', { baz => 'baz' }) would return http://localhost:5000/foo/bar?baz=baz.
http://localhost:5000/foo
request->uri_for('/bar', { baz => 'baz' })
http://localhost:5000/foo/bar?baz=baz
Called in scalar context, returns a hashref of params, either from the specified source (see below for more info on that) or merging all sources.
So, you can use, for instance:
my $foo = params->{foo}
If called in list context, returns a list of key => value pairs, so you could use:
my %allparams = params;
If a required source isn't specified, a mixed hashref (or list of key value pairs, in list context) will be returned; this will contain params from all sources (route, query, body).
In practical terms, this means that if the param foo is passed both on the querystring and in a POST body, you can only access one of them.
foo
If you want to see only params from a given source, you can say so by passing the $source param to params():
$source
params()
my %querystring_params = params('query'); my %route_params = params('route'); my %post_params = params('body');
If source equals route, then only params parsed from the route pattern are returned.
route
If source equals query, then only params parsed from the query string are returned.
query
If source equals body, then only params sent in the request body will be returned.
body
If another value is given for $source, then an exception is triggered.
Return the content type of the request.
Return the content length of the request.
Return the value of the given header, if present. If the header has multiple values, returns an the list of values if called in list context, the first one in scalar.
Return the raw body of the request, unparsed.
If you need to access the body of the request, you have to use this accessor and should not try to read psgi.input by hand. Dancer::Request already did it for you and kept the raw body untouched in there.
psgi.input
Dancer::Request
Return true if the value of the header X-Requested-With is XMLHttpRequest.
X-Requested-With
Return the current environment (%ENV), as a hashref.
%ENV
Returns a reference to a hash containing uploads. Values can be either a Dancer::Request::Upload object, or an arrayref of Dancer::Request::Upload objects.
All HTTP environment variables that are in %ENV will be provided in the Dancer::Request object through specific accessors, here are those supported:
user_agent
host
accept_language
accept_charset
accept_encoding
keep_alive
connection
accept
This module has been written by Alexis Sukrieh and was mostly inspired by Plack::Request, written by Tatsuiko Miyagawa.
Tatsuiko Miyagawa also gave a hand for the PSGI interface.
This module is released under the same terms as Perl itself.
Dancer
To install Dancer, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Dancer
CPAN shell
perl -MCPAN -e shell install Dancer
For more information on module installation, please visit the detailed CPAN module installation guide.