2 Catalyst::Controller::REST - A RESTful controller
8 package Foo::Controller::Bar;
10 use base 'Catalyst::Controller::REST';
12 sub thing : Local : ActionClass('REST') { }
14 # Answer GET requests to "thing"
16 my ( $self, $c ) = @_;
18 # Return a 200 OK, with the data in entity
19 # serialized in the body
24 foo => 'is real bar-y',
29 # Answer PUT requests to "thing"
35 Catalyst::Controller::REST implements a mechanism for building RESTful
36 services in Catalyst. It does this by extending the normal Catalyst
37 dispatch mechanism to allow for different subroutines to be called based
38 on the HTTP Method requested, while also transparently handling all the
39 serialization/deserialization for you.
41 This is probably best served by an example. In the above controller, we
42 have declared a Local Catalyst action on "sub thing", and have used the
45 Below, we have declared "thing_GET" and "thing_PUT". Any GET requests to
46 thing will be dispatched to "thing_GET", while any PUT requests will be
47 dispatched to "thing_PUT".
49 Any unimplemented HTTP methods will be met with a "405 Method Not
50 Allowed" response, automatically containing the proper list of available
51 methods. You can override this behavior through implementing a custom
52 "thing_not_implemented" method.
54 If you do not provide an OPTIONS handler, we will respond to any OPTIONS
55 requests with a "200 OK", populating the Allowed header automatically.
57 Any data included in "$c->stash->{'rest'}" will be serialized for you.
58 The serialization format will be selected based on the content-type of
59 the incoming request. It is probably easier to use the "STATUS HELPERS",
60 which are described below.
62 The HTTP POST, PUT, and OPTIONS methods will all automatically
63 deserialize the contents of $c->request->body based on the requests
64 content-type header. A list of understood serialization formats is
67 If we do not have (or cannot run) a serializer for a given content-type,
68 a 415 "Unsupported Media Type" error is generated.
70 To make your Controller RESTful, simply have it
72 use base 'Catalyst::Controller::REST';
75 Catalyst::Controller::REST will automatically serialize your responses,
76 and deserialize any POST, PUT or OPTIONS requests. It evaluates which
77 serializer to use by mapping a content-type to a Serialization module.
78 We select the content-type based on:
80 The Content-Type Header
81 If the incoming HTTP Request had a Content-Type header set, we will
84 The content-type Query Parameter
85 If this is a GET request, you can supply a content-type query
88 Evaluating the Accept Header
89 Finally, if the client provided an Accept header, we will evaluate it
90 and use the best-ranked choice.
93 A given serialization mechanism is only available if you have the
94 underlying modules installed. For example, you can't use XML::Simple if
95 it's not already installed.
97 In addition, each serializer has it's quirks in terms of what sorts of
98 data structures it will properly handle. Catalyst::Controller::REST
99 makes no attempt to svae you from yourself in this regard. :)
101 "text/x-yaml" => "YAML::Syck"
102 Returns YAML generated by YAML::Syck.
104 "text/html" => "YAML::HTML"
105 This uses YAML::Syck and URI::Find to generate YAML with all URLs
106 turned to hyperlinks. Only useable for Serialization.
108 "text/x-json" => "JSON::Syck"
109 Uses JSON::Syck to generate JSON output
111 "text/x-data-dumper" => "Data::Serializer"
112 Uses the Data::Serializer module to generate Data::Dumper output.
114 "text/x-data-denter" => "Data::Serializer"
115 Uses the Data::Serializer module to generate Data::Denter output.
117 "text/x-data-taxi" => "Data::Serializer"
118 Uses the Data::Serializer module to generate Data::Taxi output.
120 "application/x-storable" => "Data::Serializer"
121 Uses the Data::Serializer module to generate Storable output.
123 "application/x-freezethaw" => "Data::Serializer"
124 Uses the Data::Serializer module to generate FreezeThaw output.
126 "text/x-config-general" => "Data::Serializer"
127 Uses the Data::Serializer module to generate Config::General output.
129 "text/x-php-serialization" => "Data::Serializer"
130 Uses the Data::Serializer module to generate PHP::Serialization
133 "text/xml" => "XML::Simple"
134 Uses XML::Simple to generate XML output. This is probably not suitable
135 for any real heavy XML work. Due to XML::Simples requirement that the
136 data you serialize be a HASHREF, we transform outgoing data to be in
139 { data => $yourdata }
142 Uses a regular Catalyst view. For example, if you wanted to have your
143 "text/html" and "text/xml" views rendered by TT:
145 'text/html' => [ 'View', 'TT' ],
146 'text/xml' => [ 'View', 'XML' ],
148 Will do the trick nicely.
150 By default, Catalyst::Controller::REST will return a "415 Unsupported
151 Media Type" response if an attempt to use an unsupported content-type is
152 made. You can ensure that something is always returned by setting the
153 "default" config option:
155 __PACKAGE__->config->{'serialize'}->{'default'} = 'text/x-yaml';
157 Would make it always fall back to the serializer plugin defined for
160 Implementing new Serialization formats is easy! Contributions are most
161 welcome! See Catalyst::Action::Serialize and
162 Catalyst::Action::Deserialize for more information.
165 If you would like to implement a custom serializer, you should create
166 two new modules in the Catalyst::Action::Serialize and
167 Catalyst::Action::Deserialize namespace. Then assign your new class to
168 the content-type's you want, and you're done.
171 Since so much of REST is in using HTTP, we provide these Status Helpers.
172 Using them will ensure that you are responding with the proper codes,
173 headers, and entities.
175 These helpers try and conform to the HTTP 1.1 Specification. You can
176 refer to it at: <http://www.w3.org/Protocols/rfc2616/rfc2616.txt>. These
177 routines are all implemented as regular subroutines, and as such require
178 you pass the current context ($c) as the first argument.
181 Returns a "200 OK" response. Takes an "entity" to serialize.
188 radiohead => "Is a good band!",
193 Returns a "201 CREATED" response. Takes an "entity" to serialize,
194 and a "location" where the created object can be found.
198 $self->status_created(
200 location => $c->req->uri->as_string,
202 radiohead => "Is a good band!",
206 In the above example, we use the requested URI as our location. This
207 is probably what you want for most PUT requests.
210 Returns a "202 ACCEPTED" response. Takes an "entity" to serialize.
214 $self->status_accepted(
222 Returns a "400 BAD REQUEST" response. Takes a "message" argument as
223 a scalar, which will become the value of "error" in the serialized
228 $self->status_bad_request(
230 message => "Cannot do what you have asked!",
234 Returns a "404 NOT FOUND" response. Takes a "message" argument as a
235 scalar, which will become the value of "error" in the serialized
240 $self->status_not_found(
242 message => "Cannot find what you were looking for!",
246 If you want to construct your responses yourself, all you need to do is
247 put the object you want serialized in $c->stash->{'rest'}.
249 IMPLEMENTATION DETAILS
250 This Controller ties together Catalyst::Action::REST,
251 Catalyst::Action::Serialize and Catalyst::Action::Deserialize. It should
252 be suitable for most applications. You should be aware that it:
254 Configures the Serialization Actions
255 This class provides a default configuration for Serialization. It is
260 'stash_key' => 'rest',
262 'text/html' => 'YAML::HTML',
263 'text/xml' => 'XML::Simple',
264 'text/x-yaml' => 'YAML',
265 'text/x-json' => 'JSON',
266 'text/x-data-dumper' => [ 'Data::Serializer', 'Data::Dumper' ],
267 'text/x-data-denter' => [ 'Data::Serializer', 'Data::Denter' ],
268 'text/x-data-taxi' => [ 'Data::Serializer', 'Data::Taxi' ],
269 'application/x-storable' => [ 'Data::Serializer', 'Storable'
271 'application/x-freezethaw' => [ 'Data::Serializer', 'FreezeThaw'
273 'text/x-config-general' => [ 'Data::Serializer', 'Config::General' ]
275 'text/x-php-serialization' => [ 'Data::Serializer', 'PHP::Serialization' ],
280 You can read the full set of options for this configuration block in
281 Catalyst::Action::Serialize.
283 Sets a "begin" and "end" method for you
284 The "begin" method uses Catalyst::Action::Deserialize. The "end"
285 method uses Catalyst::Action::Serialize. If you want to override
286 either behavior, simply implement your own "begin" and "end" actions
289 my Foo::Controller::Monkey;
290 use base qw(Catalyst::Controller::REST);
294 ... do things before Deserializing ...
295 $self->maybe::next::method($c);
296 ... do things after Deserializing ...
301 ... do things before Serializing ...
302 $self->maybe::next::method($c);
303 ... do things after Serializing ...
307 I have code in production using Catalyst::Controller::REST. That
308 said, it is still under development, and it's possible that things
309 may change between releases. I promise to not break things
313 Catalyst::Action::REST, Catalyst::Action::Serialize,
314 Catalyst::Action::Deserialize
316 For help with REST in general:
318 The HTTP 1.1 Spec is required reading.
319 http://www.w3.org/Protocols/rfc2616/rfc2616.txt
322 http://en.wikipedia.org/wiki/Representational_State_Transfer
324 The REST Wiki: http://rest.blueoxen.net/cgi-bin/wiki.pl?FrontPage
327 Adam Jacob <adam@stalecoffee.org>, with lots of help from mst and
330 Marchex, Inc. paid me while I developed this module.
331 (http://www.marchex.com)
334 You may distribute this code under the same terms as Perl itself.