How to limit POST request in Catalyst

I am very new to Catalyst and just starting to build a web application to learn some things.

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

A specific example is a query that initiates the creation of an object in a database. Since I want this application to be fairly strict regarding REST verbs, this should only be possible through POST.

I know that I can use the $ 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 .

Now i have something like

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

Am I doomed to check a method inside a routine and do this for each method?

Please keep in mind that I am extremely new to Catalyst, so this might be a dumb question.

Thanks for the help!

+4
source share
3 answers

You can use Catalyst :: Controller :: REST .

 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', }, ); } 
+6
source
 if ( $c->req->method eq 'POST' ) { $form->process( params => $c->req->params ); } 
+2
source

Catalyst has been using the HTTP method for almost a year:

http://www.catalystframework.org/calendar/2013/3

There are pros and cons to using Catalyst :: Action :: REST. It basically comes down to style, and also CAR does a better job by setting an HTTP header (may be important for hard REST applications)

But for the basics, what you get out of the box is fine.

0
source

Source: https://habr.com/ru/post/1380462/


All Articles