Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to limit a request to POST in Catalyst

Tags:

perl

catalyst

I'm very new to Catalyst and just starting building up a web app to learn some stuff.

One thing that I haven't figured out is how to limit the requests to a given controller method to POST (for instance).

A concrete example would be, the request that will trigger the creation of an object in the Database. Since I want this app to be pretty strict regarding REST verbs, this should only be possible via POST.

I know that I can use $c->method to check the method used in the request, and return an error or something like that if I don't find what I'm looking for, but I was hoping there was a... cleaner way.

Right now I have something like

sub create :Local :Args(0) {
    ...
}

Am I doomed to check the method inside the subroutine, and do this for every method there is?

Please do keep in mind that I'm extremely new to Catalyst, so this might be a stupid question.

Thanks for the help!

like image 536
pcalcao Avatar asked Feb 23 '23 11:02

pcalcao


2 Answers

You can use the Catalyst::Controller::REST module.

sub thing : Local : ActionClass('REST') { }

# Answer POST requests to "thing"
sub thing_POST {
   my ( $self, $c ) = @_;

   # Return a 200 OK, with the data in entity
   # serialized in the body
   $self->status_ok(
        $c,
        entity => {
            some => 'data',
            foo  => 'is real bar-y',
        },
   );
}
like image 125
Quentin Avatar answered Mar 03 '23 13:03

Quentin


if ( $c->req->method eq 'POST' ) {
    $form->process( params => $c->req->params );
}
like image 42
Raony Guimarães Avatar answered Mar 03 '23 11:03

Raony Guimarães