2 Catalyst::Controller::REST - A RESTful controller
5 package Foo::Controller::Bar;
7 use base 'Catalyst::Controller::REST';
9 sub thing : Local : ActionClass('REST') { }
11 # Answer GET requests to "thing"
13 my ( $self, $c ) = @_;
15 # Return a 200 OK, with the data in entity
16 # serialized in the body
21 foo => 'is real bar-y',
26 # Answer PUT requests to "thing"
32 Catalyst::Controller::REST implements a mechanism for building RESTful
33 services in Catalyst. It does this by extending the normal Catalyst
34 dispatch mechanism to allow for different subroutines to be called based
35 on the HTTP Method requested, while also transparently handling all the
36 serialization/deserialization for you.
38 This is probably best served by an example. In the above controller, we
39 have declared a Local Catalyst action on "sub thing", and have used the
42 Below, we have declared "thing_GET" and "thing_PUT". Any GET requests to
43 thing will be dispatched to "thing_GET", while any PUT requests will be
44 dispatched to "thing_PUT".
46 Any unimplemented HTTP methods will be met with a "405 Method Not
47 Allowed" response, automatically containing the proper list of available
48 methods. You can override this behavior through implementing a custom
49 "thing_not_implemented" method.
51 If you do not provide an OPTIONS handler, we will respond to any OPTIONS
52 requests with a "200 OK", populating the Allowed header automatically.
54 Any data included in "$c->stash->{'rest'}" will be serialized for you.
55 The serialization format will be selected based on the content-type of
56 the incoming request. It is probably easier to use the "STATUS HELPERS",
57 which are described below.
59 The HTTP POST, PUT, and OPTIONS methods will all automatically
60 deserialize the contents of $c->request->body based on the requests
61 content-type header. A list of understood serialization formats is
64 If we do not have (or cannot run) a serializer for a given content-type,
65 a 415 "Unsupported Media Type" error is generated.
67 To make your Controller RESTful, simply have it
69 use base 'Catalyst::Controller::REST';
72 Catalyst::Controller::REST will automatically serialize your responses,
73 and deserialize any POST, PUT or OPTIONS requests. It evaluates which
74 serializer to use by mapping a content-type to a Serialization module.
75 We select the content-type based on:
77 The Content-Type Header
78 If the incoming HTTP Request had a Content-Type header set, we will
81 The content-type Query Parameter
82 If this is a GET request, you can supply a content-type query
85 Evaluating the Accept Header
86 Finally, if the client provided an Accept header, we will evaluate it
87 and use the best-ranked choice.
90 A given serialization mechanism is only available if you have the
91 underlying modules installed. For example, you can't use XML::Simple if
92 it's not already installed.
94 In addition, each serializer has it's quirks in terms of what sorts of
95 data structures it will properly handle. Catalyst::Controller::REST
96 makes no attempt to svae you from yourself in this regard. :)
98 "text/x-yaml" => "YAML::Syck"
99 Returns YAML generated by YAML::Syck.
101 "text/html" => "YAML::HTML"
102 This uses YAML::Syck and URI::Find to generate YAML with all URLs
103 turned to hyperlinks. Only useable for Serialization.
105 "text/x-json" => "JSON::Syck"
106 Uses JSON::Syck to generate JSON output
108 "text/x-data-dumper" => "Data::Serializer"
109 Uses the Data::Serializer module to generate Data::Dumper output.
111 "text/x-data-denter" => "Data::Serializer"
112 Uses the Data::Serializer module to generate Data::Denter output.
114 "text/x-data-taxi" => "Data::Serializer"
115 Uses the Data::Serializer module to generate Data::Taxi output.
117 "application/x-storable" => "Data::Serializer"
118 Uses the Data::Serializer module to generate Storable output.
120 "application/x-freezethaw" => "Data::Serializer"
121 Uses the Data::Serializer module to generate FreezeThaw output.
123 "text/x-config-general" => "Data::Serializer"
124 Uses the Data::Serializer module to generate Config::General output.
126 "text/x-php-serialization" => "Data::Serializer"
127 Uses the Data::Serializer module to generate PHP::Serialization
130 "text/xml" => "XML::Simple"
131 Uses XML::Simple to generate XML output. This is probably not suitable
132 for any real heavy XML work. Due to XML::Simples requirement that the
133 data you serialize be a HASHREF, we transform outgoing data to be in
136 { data => $yourdata }
139 Uses a regular Catalyst view. For example, if you wanted to have your
140 "text/html" and "text/xml" views rendered by TT:
142 'text/html' => [ 'View', 'TT' ],
143 'text/xml' => [ 'View', 'XML' ],
145 Will do the trick nicely.
147 By default, Catalyst::Controller::REST will return a "415 Unsupported
148 Media Type" response if an attempt to use an unsupported content-type is
149 made. You can ensure that something is always returned by setting the
150 "default" config option:
152 __PACKAGE__->config->{'serialize'}->{'default'} = 'text/x-yaml';
154 Would make it always fall back to the serializer plugin defined for
157 Implementing new Serialization formats is easy! Contributions are most
158 welcome! See Catalyst::Action::Serialize and
159 Catalyst::Action::Deserialize for more information.
162 If you would like to implement a custom serializer, you should create
163 two new modules in the Catalyst::Action::Serialize and
164 Catalyst::Action::Deserialize namespace. Then assign your new class to
165 the content-type's you want, and you're done.
168 Since so much of REST is in using HTTP, we provide these Status Helpers.
169 Using them will ensure that you are responding with the proper codes,
170 headers, and entities.
172 These helpers try and conform to the HTTP 1.1 Specification. You can
173 refer to it at: <http://www.w3.org/Protocols/rfc2616/rfc2616.txt>. These
174 routines are all implemented as regular subroutines, and as such require
175 you pass the current context ($c) as the first argument.
178 Returns a "200 OK" response. Takes an "entity" to serialize.
185 radiohead => "Is a good band!",
190 Returns a "201 CREATED" response. Takes an "entity" to serialize,
191 and a "location" where the created object can be found.
195 $self->status_created(
197 location => $c->req->uri->as_string,
199 radiohead => "Is a good band!",
203 In the above example, we use the requested URI as our location. This
204 is probably what you want for most PUT requests.
207 Returns a "202 ACCEPTED" response. Takes an "entity" to serialize.
211 $self->status_accepted(
219 Returns a "400 BAD REQUEST" response. Takes a "message" argument as
220 a scalar, which will become the value of "error" in the serialized
225 $self->status_bad_request(
227 message => "Cannot do what you have asked!",
231 Returns a "404 NOT FOUND" response. Takes a "message" argument as a
232 scalar, which will become the value of "error" in the serialized
237 $self->status_not_found(
239 message => "Cannot find what you were looking for!",
243 If you want to construct your responses yourself, all you need to do is
244 put the object you want serialized in $c->stash->{'rest'}.
246 IMPLEMENTATION DETAILS
247 This Controller ties together Catalyst::Action::REST,
248 Catalyst::Action::Serialize and Catalyst::Action::Deserialize. It should
249 be suitable for most applications. You should be aware that it:
251 Configures the Serialization Actions
252 This class provides a default configuration for Serialization. It is
257 'stash_key' => 'rest',
259 'text/html' => 'YAML::HTML',
260 'text/xml' => 'XML::Simple',
261 'text/x-yaml' => 'YAML',
262 'text/x-json' => 'JSON',
263 'text/x-data-dumper' => [ 'Data::Serializer', 'Data::Dumper' ],
264 'text/x-data-denter' => [ 'Data::Serializer', 'Data::Denter' ],
265 'text/x-data-taxi' => [ 'Data::Serializer', 'Data::Taxi' ],
266 'application/x-storable' => [ 'Data::Serializer', 'Storable'
268 'application/x-freezethaw' => [ 'Data::Serializer', 'FreezeThaw'
270 'text/x-config-general' => [ 'Data::Serializer', 'Config::General' ]
272 'text/x-php-serialization' => [ 'Data::Serializer', 'PHP::Serialization' ],
277 You can read the full set of options for this configuration block in
278 Catalyst::Action::Serialize.
280 Sets a "begin" and "end" method for you
281 The "begin" method uses Catalyst::Action::Deserialize. The "end"
282 method uses Catalyst::Action::Serialize. If you want to override
283 either behavior, simply implement your own "begin" and "end" actions
286 my Foo::Controller::Monkey;
287 use base qw(Catalyst::Controller::REST);
291 ... do things before Deserializing ...
292 $self->NEXT::begin($c);
293 ... do things after Deserializing ...
298 ... do things before Serializing ...
299 $self->NEXT::end($c);
300 ... do things after Serializing ...
304 I have code in production using Catalyst::Controller::REST. That
305 said, it is still under development, and it's possible that things
306 may change between releases. I promise to not break things
310 Catalyst::Action::REST, Catalyst::Action::Serialize,
311 Catalyst::Action::Deserialize
313 For help with REST in general:
315 The HTTP 1.1 Spec is required reading.
316 http://www.w3.org/Protocols/rfc2616/rfc2616.txt
319 http://en.wikipedia.org/wiki/Representational_State_Transfer
321 The REST Wiki: http://rest.blueoxen.net/cgi-bin/wiki.pl?FrontPage
324 Adam Jacob <adam@stalecoffee.org>, with lots of help from mst and
327 Marchex, Inc. paid me while I developed this module.
328 (http://www.marchex.com)
331 You may distribute this code under the same terms as Perl itself.