Get HTTP method

To get the HTTP method, use the method method of the Mojo::Message::Request class.

my $method = $req->method;

The following is an example with Mojolicious::Lite. You can access the Mojo::Message::Request object using the req method of the Mojolicious::Controller object.

#Mojolicious::Lite
get'/' => sub {
   my $self = shift;
   my $method = $self->req->method;
};;

The following is an example from Mojolicious.

#Mojolicious
package MyApp::Picture;

use Mojo::Base'Mojolicious::Controller';

use utf8;

# Login screen
sub list {
   my $self = shift;
   my $method = $self->req->method;
}

When branching by the HTTP method, it is better to make a judgment after making it lowercase with the lc function as follows.

if (lc $self->req->methodeq'post') {
   ...
}
else {
   ...
}

Associated Information