1 package Catalyst::Controller::REST;
3 use namespace::autoclean;
9 Catalyst::Controller::REST - A RESTful controller
13 package Foo::Controller::Bar;
15 use namespace::autoclean;
17 BEGIN { extends 'Catalyst::Controller::REST' }
19 sub thing : Local : ActionClass('REST') { }
21 # Answer GET requests to "thing"
23 my ( $self, $c ) = @_;
25 # Return a 200 OK, with the data in entity
26 # serialized in the body
31 foo => 'is real bar-y',
36 # Answer PUT requests to "thing"
38 my ( $self, $c ) = @_;
40 $radiohead = $c->req->data->{radiohead};
42 $self->status_created(
44 location => $c->req->uri,
46 radiohead => $radiohead,
53 Catalyst::Controller::REST implements a mechanism for building
54 RESTful services in Catalyst. It does this by extending the
55 normal Catalyst dispatch mechanism to allow for different
56 subroutines to be called based on the HTTP Method requested,
57 while also transparently handling all the serialization/deserialization for
60 This is probably best served by an example. In the above
61 controller, we have declared a Local Catalyst action on
62 "sub thing", and have used the ActionClass('REST').
64 Below, we have declared "thing_GET" and "thing_PUT". Any
65 GET requests to thing will be dispatched to "thing_GET",
66 while any PUT requests will be dispatched to "thing_PUT".
68 Any unimplemented HTTP methods will be met with a "405 Method Not Allowed"
69 response, automatically containing the proper list of available methods. You
70 can override this behavior through implementing a custom
71 C<thing_not_implemented> method.
73 If you do not provide an OPTIONS handler, we will respond to any OPTIONS
74 requests with a "200 OK", populating the Allowed header automatically.
76 Any data included in C<< $c->stash->{'rest'} >> will be serialized for you.
77 The serialization format will be selected based on the content-type
78 of the incoming request. It is probably easier to use the L<STATUS HELPERS>,
79 which are described below.
81 "The HTTP POST, PUT, and OPTIONS methods will all automatically
82 L<deserialize|Catalyst::Action::Deserialize> the contents of
83 C<< $c->request->body >> into the C<< $c->request->data >> hashref", based on
84 the request's C<Content-type> header. A list of understood serialization
85 formats is L<below|/AVAILABLE SERIALIZERS>.
87 If we do not have (or cannot run) a serializer for a given content-type, a 415
88 "Unsupported Media Type" error is generated.
90 To make your Controller RESTful, simply have it
92 BEGIN { extends 'Catalyst::Controller::REST' }
96 See L<Catalyst::Action::Serialize/CONFIGURATION>. Note that the C<serialize>
97 key has been deprecated.
101 Catalyst::Controller::REST will automatically serialize your
102 responses, and deserialize any POST, PUT or OPTIONS requests. It evaluates
103 which serializer to use by mapping a content-type to a Serialization module.
104 We select the content-type based on:
108 =item B<The Content-Type Header>
110 If the incoming HTTP Request had a Content-Type header set, we will use it.
112 =item B<The content-type Query Parameter>
114 If this is a GET request, you can supply a content-type query parameter.
116 =item B<Evaluating the Accept Header>
118 Finally, if the client provided an Accept header, we will evaluate
119 it and use the best-ranked choice.
123 =head1 AVAILABLE SERIALIZERS
125 A given serialization mechanism is only available if you have the underlying
126 modules installed. For example, you can't use XML::Simple if it's not already
129 In addition, each serializer has its quirks in terms of what sorts of data
130 structures it will properly handle. L<Catalyst::Controller::REST> makes
131 no attempt to save you from yourself in this regard. :)
135 =item * C<text/x-yaml> => C<YAML::Syck>
137 Returns YAML generated by L<YAML::Syck>.
139 =item * C<text/html> => C<YAML::HTML>
141 This uses L<YAML::Syck> and L<URI::Find> to generate YAML with all URLs turned
142 to hyperlinks. Only usable for Serialization.
144 =item * C<application/json> => C<JSON>
146 Uses L<JSON> to generate JSON output. It is strongly advised to also have
147 L<JSON::XS> installed. The C<text/x-json> content type is supported but is
148 deprecated and you will receive warnings in your log.
150 You can also add a hash in your controller config to pass options to the json object.
151 For instance, to relax permissions when deserializing input, add:
153 json_options => { relaxed => 1 }
156 =item * C<text/javascript> => C<JSONP>
158 If a callback=? parameter is passed, this returns javascript in the form of: $callback($serializedJSON);
160 Note - this is disabled by default as it can be a security risk if you are unaware.
162 The usual MIME types for this serialization format are: 'text/javascript', 'application/x-javascript',
163 'application/javascript'.
165 =item * C<text/x-data-dumper> => C<Data::Serializer>
167 Uses the L<Data::Serializer> module to generate L<Data::Dumper> output.
169 =item * C<text/x-data-denter> => C<Data::Serializer>
171 Uses the L<Data::Serializer> module to generate L<Data::Denter> output.
173 =item * C<text/x-data-taxi> => C<Data::Serializer>
175 Uses the L<Data::Serializer> module to generate L<Data::Taxi> output.
177 =item * C<text/x-config-general> => C<Data::Serializer>
179 Uses the L<Data::Serializer> module to generate L<Config::General> output.
181 =item * C<text/x-php-serialization> => C<Data::Serializer>
183 Uses the L<Data::Serializer> module to generate L<PHP::Serialization> output.
185 =item * C<text/xml> => C<XML::Simple>
187 Uses L<XML::Simple> to generate XML output. This is probably not suitable
188 for any real heavy XML work. Due to L<XML::Simple>s requirement that the data
189 you serialize be a HASHREF, we transform outgoing data to be in the form of:
191 { data => $yourdata }
195 Uses a regular Catalyst view. For example, if you wanted to have your
196 C<text/html> and C<text/xml> views rendered by TT, set:
200 'text/html' => [ 'View', 'TT' ],
201 'text/xml' => [ 'View', 'XML' ],
205 Your views should have a C<process> method like this:
208 my ( $self, $c, $stash_key ) = @_;
212 $output = $self->serialize( $c->stash->{$stash_key} );
216 $c->response->body( $output );
217 return 1; # important
221 my ( $self, $data ) = @_;
223 my $serialized = ... process $data here ...
230 For infinite flexibility, you can provide a callback for the
231 deserialization/serialization steps.
235 'text/xml' => [ 'Callback', { deserialize => \&parse_xml, serialize => \&render_xml } ],
239 The C<deserialize> callback is passed a string that is the body of the
240 request and is expected to return a scalar value that results from
241 the deserialization. The C<serialize> callback is passed the data
242 structure that needs to be serialized and must return a string suitable
243 for returning in the HTTP response. In addition to receiving the scalar
244 to act on, both callbacks are passed the controller object and the context
245 (i.e. C<$c>) as the second and third arguments.
249 By default, L<Catalyst::Controller::REST> will return a
250 C<415 Unsupported Media Type> response if an attempt to use an unsupported
251 content-type is made. You can ensure that something is always returned by
252 setting the C<default> config option:
254 __PACKAGE__->config(default => 'text/x-yaml');
256 would make it always fall back to the serializer plugin defined for
259 =head1 CUSTOM SERIALIZERS
261 Implementing new Serialization formats is easy! Contributions
262 are most welcome! If you would like to implement a custom serializer,
263 you should create two new modules in the L<Catalyst::Action::Serialize>
264 and L<Catalyst::Action::Deserialize> namespace. Then assign your new
265 class to the content-type's you want, and you're done.
267 See L<Catalyst::Action::Serialize> and L<Catalyst::Action::Deserialize>
268 for more information.
270 =head1 STATUS HELPERS
272 Since so much of REST is in using HTTP, we provide these Status Helpers.
273 Using them will ensure that you are responding with the proper codes,
274 headers, and entities.
276 These helpers try and conform to the HTTP 1.1 Specification. You can
277 refer to it at: L<http://www.w3.org/Protocols/rfc2616/rfc2616.txt>.
278 These routines are all implemented as regular subroutines, and as
279 such require you pass the current context ($c) as the first argument.
285 BEGIN { extends 'Catalyst::Controller' }
286 use Params::Validate qw(SCALAR OBJECT);
288 __PACKAGE__->mk_accessors(qw(serialize));
291 'stash_key' => 'rest',
293 'text/xml' => 'XML::Simple',
294 'application/json' => 'JSON',
295 'text/x-json' => 'JSON',
299 sub begin : ActionClass('Deserialize') { }
301 sub end : ActionClass('Serialize') { }
305 Returns a "200 OK" response. Takes an "entity" to serialize.
312 radiohead => "Is a good band!",
321 my %p = Params::Validate::validate( @_, { entity => 1, }, );
323 $c->response->status(200);
324 $self->_set_entity( $c, $p{'entity'} );
330 Returns a "201 CREATED" response. Takes an "entity" to serialize,
331 and a "location" where the created object can be found.
335 $self->status_created(
337 location => $c->req->uri,
339 radiohead => "Is a good band!",
343 In the above example, we use the requested URI as our location.
344 This is probably what you want for most PUT requests.
351 my %p = Params::Validate::validate(
354 location => { type => SCALAR | OBJECT },
355 entity => { optional => 1 },
359 $c->response->status(201);
360 $c->response->header( 'Location' => $p{location} );
361 $self->_set_entity( $c, $p{'entity'} );
365 =item status_accepted
367 Returns a "202 ACCEPTED" response. Takes an "entity" to serialize.
368 Also takes optional "location" for queue type scenarios.
372 $self->status_accepted(
374 location => $c->req->uri,
382 sub status_accepted {
385 my %p = Params::Validate::validate(
388 location => { type => SCALAR | OBJECT, optional => 1 },
393 $c->response->status(202);
394 $c->response->header( 'Location' => $p{location} ) if exists $p{location};
395 $self->_set_entity( $c, $p{'entity'} );
399 =item status_no_content
401 Returns a "204 NO CONTENT" response.
405 sub status_no_content {
408 $c->response->status(204);
409 $self->_set_entity( $c, undef );
413 =item status_multiple_choices
415 Returns a "300 MULTIPLE CHOICES" response. Takes an "entity" to serialize, which should
416 provide list of possible locations. Also takes optional "location" for preferred choice.
420 sub status_multiple_choices {
423 my %p = Params::Validate::validate(
427 location => { type => SCALAR | OBJECT, optional => 1 },
431 $c->response->status(300);
432 $c->response->header( 'Location' => $p{location} ) if exists $p{'location'};
433 $self->_set_entity( $c, $p{'entity'} );
439 Returns a "302 FOUND" response. Takes an "entity" to serialize.
440 Also takes optional "location".
447 my %p = Params::Validate::validate(
451 location => { type => SCALAR | OBJECT, optional => 1 },
455 $c->response->status(302);
456 $c->response->header( 'Location' => $p{location} ) if exists $p{'location'};
457 $self->_set_entity( $c, $p{'entity'} );
461 =item status_bad_request
463 Returns a "400 BAD REQUEST" response. Takes a "message" argument
464 as a scalar, which will become the value of "error" in the serialized
469 $self->status_bad_request(
471 message => "Cannot do what you have asked!",
476 sub status_bad_request {
479 my %p = Params::Validate::validate( @_, { message => { type => SCALAR }, }, );
481 $c->response->status(400);
482 $c->log->debug( "Status Bad Request: " . $p{'message'} ) if $c->debug;
483 $self->_set_entity( $c, { error => $p{'message'} } );
487 =item status_forbidden
489 Returns a "403 FORBIDDEN" response. Takes a "message" argument
490 as a scalar, which will become the value of "error" in the serialized
495 $self->status_forbidden(
497 message => "access denied",
502 sub status_forbidden {
505 my %p = Params::Validate::validate( @_, { message => { type => SCALAR }, }, );
507 $c->response->status(403);
508 $c->log->debug( "Status Forbidden: " . $p{'message'} ) if $c->debug;
509 $self->_set_entity( $c, { error => $p{'message'} } );
513 =item status_not_found
515 Returns a "404 NOT FOUND" response. Takes a "message" argument
516 as a scalar, which will become the value of "error" in the serialized
521 $self->status_not_found(
523 message => "Cannot find what you were looking for!",
528 sub status_not_found {
531 my %p = Params::Validate::validate( @_, { message => { type => SCALAR }, }, );
533 $c->response->status(404);
534 $c->log->debug( "Status Not Found: " . $p{'message'} ) if $c->debug;
535 $self->_set_entity( $c, { error => $p{'message'} } );
541 Returns a "41O GONE" response. Takes a "message" argument as a scalar,
542 which will become the value of "error" in the serialized response.
548 message => "The document have been deleted by foo",
556 my %p = Params::Validate::validate( @_, { message => { type => SCALAR }, }, );
558 $c->response->status(410);
559 $c->log->debug( "Status Gone " . $p{'message'} ) if $c->debug;
560 $self->_set_entity( $c, { error => $p{'message'} } );
568 if ( defined($entity) ) {
569 $c->stash->{ $self->{'stash_key'} } = $entity;
576 =head1 MANUAL RESPONSES
578 If you want to construct your responses yourself, all you need to
579 do is put the object you want serialized in $c->stash->{'rest'}.
581 =head1 IMPLEMENTATION DETAILS
583 This Controller ties together L<Catalyst::Action::REST>,
584 L<Catalyst::Action::Serialize> and L<Catalyst::Action::Deserialize>. It should be suitable for most applications. You should be aware that it:
588 =item Configures the Serialization Actions
590 This class provides a default configuration for Serialization. It is currently:
593 'stash_key' => 'rest',
595 'text/html' => 'YAML::HTML',
596 'text/xml' => 'XML::Simple',
597 'text/x-yaml' => 'YAML',
598 'application/json' => 'JSON',
599 'text/x-json' => 'JSON',
600 'text/x-data-dumper' => [ 'Data::Serializer', 'Data::Dumper' ],
601 'text/x-data-denter' => [ 'Data::Serializer', 'Data::Denter' ],
602 'text/x-data-taxi' => [ 'Data::Serializer', 'Data::Taxi' ],
603 'application/x-storable' => [ 'Data::Serializer', 'Storable' ],
604 'application/x-freezethaw' => [ 'Data::Serializer', 'FreezeThaw' ],
605 'text/x-config-general' => [ 'Data::Serializer', 'Config::General' ],
606 'text/x-php-serialization' => [ 'Data::Serializer', 'PHP::Serialization' ],
610 You can read the full set of options for this configuration block in
611 L<Catalyst::Action::Serialize>.
613 =item Sets a C<begin> and C<end> method for you
615 The C<begin> method uses L<Catalyst::Action::Deserialize>. The C<end>
616 method uses L<Catalyst::Action::Serialize>. If you want to override
617 either behavior, simply implement your own C<begin> and C<end> actions
618 and forward to another action with the Serialize and/or Deserialize
621 package Foo::Controller::Monkey;
623 use namespace::autoclean;
625 BEGIN { extends 'Catalyst::Controller::REST' }
627 sub begin : Private {
629 ... do things before Deserializing ...
630 $c->forward('deserialize');
631 ... do things after Deserializing ...
634 sub deserialize : ActionClass('Deserialize') {}
638 ... do things before Serializing ...
639 $c->forward('serialize');
640 ... do things after Serializing ...
643 sub serialize : ActionClass('Serialize') {}
645 If you need to deserialize multipart requests (i.e. REST data in
646 one part and file uploads in others) you can do so by using the
647 L<Catalyst::Action::DeserializeMultiPart> action class.
651 =head1 A MILD WARNING
653 I have code in production using L<Catalyst::Controller::REST>. That said,
654 it is still under development, and it's possible that things may change
655 between releases. I promise to not break things unnecessarily. :)
659 L<Catalyst::Action::REST>, L<Catalyst::Action::Serialize>,
660 L<Catalyst::Action::Deserialize>
662 For help with REST in general:
664 The HTTP 1.1 Spec is required reading. http://www.w3.org/Protocols/rfc2616/rfc2616.txt
666 Wikipedia! http://en.wikipedia.org/wiki/Representational_State_Transfer
668 The REST Wiki: http://rest.blueoxen.net/cgi-bin/wiki.pl?FrontPage
672 See L<Catalyst::Action::REST> for authors.
676 You may distribute this code under the same terms as Perl itself.
680 __PACKAGE__->meta->make_immutable;