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!
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',
},
);
}
if ( $c->req->method eq 'POST' ) {
$form->process( params => $c->req->params );
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With