-
Notifications
You must be signed in to change notification settings - Fork 0
Request data
Request data (GET or POST parameters) can be accessed using the param
method of a Mojolicious::Controller object. Similarly to the param method in CGI, you can call param without any arguments to get a list of the parameter names, or with an argument to get the value(s) of the parameter with that name:
# Mojolicious::Lite example - :
post '/form/:test' => sub {
my $self = shift; # our first parameter is a Mojolicious::Controller
my @params = $self->param; # anything in the query string, any form fields, and the "test" part of the route
foreach my $field (@params) {
my @values = $self->param($field);
....
}
};
Note that the param
method of Mojolicious::Controller lets you access parameters from 3 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
To get access to request data of the current transaction, you can call the req method on the controller object via
my $request_object = $self->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 ($self->req
) provides a param
method that works just 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.
To get a specific value that has been submitted via POST, call
my $name = $self->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 first one, as in the previous example. Just use the array context:
my @names = $self->req->body_params->param('name');
Calling $self->req->body_params
provides an instance of Mojo::Parameters. You might look there for more in depth documentation.
For GET data, use the query_params method of the request:
my $name = $self->req->query_params->param('name');
Or, for multiple values:
my @named = $self->req->query_params->param('name');
Calling $self->req->query_params
provides an instance of Mojo::Parameters.
You can convert parameters to hash reference data by to_hash()
of Mojo::Parameters.
my $param = $self->req->body_params->to_hash;
You can get Mojo::Headers object by headers()
of Mojo::Message.
my $headers = $self->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 = $self->req->headers->header('X-Forwarded-Host');
http://corky.net/dotan/programming/mojolicious-request-parameters-example.html - an even more verbose explanation