Catalyst-Action-REST
view release on metacpan or search on metacpan
lib/Catalyst/Controller/REST.pm view on Meta::CPAN
package Catalyst::Controller::REST;
$Catalyst::Controller::REST::VERSION = '1.22';
use Moose;
use namespace::autoclean;
=head1 NAME
Catalyst::Controller::REST - A RESTful controller
=head1 SYNOPSIS
package Foo::Controller::Bar;
use Moose;
use namespace::autoclean;
BEGIN { extends 'Catalyst::Controller::REST' }
sub thing : Local : ActionClass('REST') { }
# Answer GET requests to "thing"
sub thing_GET {
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',
},
);
}
# Answer PUT requests to "thing"
sub thing_PUT {
my ( $self, $c ) = @_;
$radiohead = $c->req->data->{radiohead};
$self->status_created(
$c,
location => $c->req->uri,
entity => {
radiohead => $radiohead,
}
);
}
=head1 DESCRIPTION
Catalyst::Controller::REST implements a mechanism for building
RESTful services in Catalyst. It does this by extending the
normal Catalyst dispatch mechanism to allow for different
subroutines to be called based on the HTTP Method requested,
while also transparently handling all the serialization/deserialization for
you.
This is probably best served by an example. In the above
controller, we have declared a Local Catalyst action on
"sub thing", and have used the ActionClass('REST').
Below, we have declared "thing_GET" and "thing_PUT". Any
GET requests to thing will be dispatched to "thing_GET",
while any PUT requests will be dispatched to "thing_PUT".
Any unimplemented HTTP methods will be met with a "405 Method Not Allowed"
response, automatically containing the proper list of available methods. You
can override this behavior through implementing a custom
C<thing_not_implemented> method.
If you do not provide an OPTIONS handler, we will respond to any OPTIONS
requests with a "200 OK", populating the Allowed header automatically.
Any data included in C<< $c->stash->{'rest'} >> will be serialized for you.
The serialization format will be selected based on the content-type
of the incoming request. It is probably easier to use the L<STATUS HELPERS>,
which are described below.
"The HTTP POST, PUT, and OPTIONS methods will all automatically
L<deserialize|Catalyst::Action::Deserialize> the contents of
C<< $c->request->body >> into the C<< $c->request->data >> hashref", based on
the request's C<Content-type> header. A list of understood serialization
formats is L<below|/AVAILABLE SERIALIZERS>.
If we do not have (or cannot run) a serializer for a given content-type, a 415
"Unsupported Media Type" error is generated.
To make your Controller RESTful, simply have it
BEGIN { extends 'Catalyst::Controller::REST' }
=head1 CONFIGURATION
See L<Catalyst::Action::Serialize/CONFIGURATION>. Note that the C<serialize>
key has been deprecated.
=head1 SERIALIZATION
Catalyst::Controller::REST will automatically serialize your
responses, and deserialize any POST, PUT or OPTIONS requests. It evaluates
which serializer to use by mapping a content-type to a Serialization module.
We select the content-type based on:
=over
=item B<The Content-Type Header>
If the incoming HTTP Request had a Content-Type header set, we will use it.
=item B<The content-type Query Parameter>
If this is a GET request, you can supply a content-type query parameter.
=item B<Evaluating the Accept Header>
( run in 0.920 second using v1.01-cache-2.11-cpan-cdf2f3d4e48 )