Request data

Dan Book edited this page Apr 12, 2016 · 13 revisions

Request Data

Request data (GET or POST parameters) can be accessed using the param and every_param methods of a Mojolicious::Controller object. Unlike the param method in CGI, param will only return a single value of the named parameter, and every_param will return an array reference of all values of the named parameter, to avoid subtle mistakes caused by context sensitivity. To get the names of all request parameters, use the params method of the request object.

   # Mojolicious::Lite example - :
   post '/form/:test' => sub {
    my $c = shift; # our first parameter is a Mojolicious::Controller
     foreach my $field ('test', 'param1', 'param2', 'param3') { # gets the placeholder value first
          my $last_value = $c->param($field);
          my @all_values = @{$c->every_param($field)};
          ....
     }
   };

Note that the param and every_param methods of Mojolicious::Controller let you access parameters from 4 sources:

1 - GET parameters (from the query string part of the url)

2 - POST parameters (for example from HTML form fields)

3 - Route placeholders - capture expressions from the matching route

4 - File uploads (as Mojo::Upload objects)

Request Object

To get access to request data of the current transaction, you can call the req method on the controller object via

    my $request_object = $c->req;

The request object is an instance of Mojo::Message::Request which inherits from Mojo::Message. You might look there for more in depth documentation.

The request object ($c->req) provides a param method that works just like the controller's method, except that it only exposes POST and GET parameters.

If you want only the POST or the GET parameters, you can get at them by calling $req->body_params (defined in Mojo::Message) or $req->query_params (defined in Mojo::Message::Request) respectively. Both methods provides an instance of Mojo::Parameters, which has a param method that works as described above, and a names method which can be used to retrieve the names of all parameters, similar to calling CGI's param method with no arguments. You can also retrieve a Mojo::Parameters object for the combined GET and POST parameters by calling $req->params.

    my @all_param_names = @{$c->req->params->names};

POST Data

To get a specific value that has been submitted via POST, call

    my $name = $c->req->body_params->param('name');

As there might be more than one value for each parameter key, you can also get all parameter values for a specific key, not only the last one, as in the previous example. Just use every_param:

    my @names = @{$c->req->body_params->every_param('name')};

Calling $c->req->body_params provides an instance of Mojo::Parameters. You might look there for more in depth documentation.

It's also worth pointing out that you can decode a hashref of POSTed JSON data with:

    my $data = $c->req->json;

GET Data

For GET data, use the query_params method of the request:

    my $name = $c->req->query_params->param('name');

Or, for multiple values:

       my @named = @{$c->req->query_params->every_param('name')};

Calling $c->req->query_params provides an instance of Mojo::Parameters.

Convert Parameters To A Hash Reference

You can convert parameters to a hash reference by calling to_hash() of Mojo::Parameters:

    my $param = $c->req->params->to_hash;

This also works on the GET and POST specific methods:

    my $param = $c->req->query_params->to_hash;
    my $param = $c->req->body_params->to_hash;

To create nested data structures from request parameters use one of the following plugins: Mojolicious::Plugin::GroupedParams, Mojolicious::Plugin::ParamExpand

HTTP Headers

You can get Mojo::Headers object by headers() of Mojo::Message.

    my $headers = $c->req->headers;
    my $content_type = $headers->content_type;

There are many methods to get header value.

    accept_language(), accept_ranges(), authorization(),
    connection(), content_disposition(), content_length(),
    content_range(), content_transfer_encoding(),

    content_type(), cookie(), date(), expect(),
    host(), if_modified_since(), last_modified(),
    location(), origin(), proxy_authenticate(),

    proxy_authorization(), range(), referrer(),
    sec_websocket_key1(), sec_websocket_key2(),
    sec_websocket_location(), sec_websocket_origin(),

    sec_websocket_protocol(), server(),
    transfer_encoding(), upgrade(), user_agent(),
    www_authenticate()

You can also get any header by header() of Mojo::Headers

    my $x_forwarded_host = $c->req->headers->header('X-Forwarded-Host');

Further Reading

http://corky.net/dotan/programming/mojolicious-request-parameters-example.html - an even more verbose explanation