3 # Created by: Adam Jacob, Marchex, <adam@hjksolutions.com>
4 # Created on: 10/12/2006 03:00:32 PM PDT
7 package Catalyst::Action::REST;
10 use Moose::Util qw(does_role);
11 use Catalyst::RequestRole::REST;
12 use Catalyst::Controller::REST;
13 use namespace::clean -except => 'meta';
15 extends 'Catalyst::Action';
17 BEGIN { require 5.008001; }
19 our $VERSION = '0.74';
23 Catalyst::Action::REST - Automated REST Method Dispatching
27 sub foo :Local :ActionClass('REST') {
28 ... do setup for HTTP method specific handlers ...
32 ... do something for GET requests ...
36 ... do somethign for PUT requests ...
41 This Action handles doing automatic method dispatching for REST requests. It
42 takes a normal Catalyst action, and changes the dispatch to append an
43 underscore and method name.
45 For example, in the synopsis above, calling GET on "/foo" would result in
46 the foo_GET method being dispatched.
48 If a method is requested that is not implemented, this action will
49 return a status 405 (Method Not Found). It will populate the "Allow" header
50 with the list of implemented request methods. You can override this behavior
51 by implementing a custom 405 handler like so:
53 sub foo_not_implemented {
54 ... handle not implemented methods ...
57 If you do not provide an _OPTIONS subroutine, we will automatically respond
58 with a 200 OK. The "Allow" header will be populated with the list of
59 implemented request methods.
61 It is likely that you really want to look at L<Catalyst::Controller::REST>,
62 which brings this class together with automatic Serialization of requests
65 When you use this module, the request class will be changed to
66 L<Catalyst::Request::REST>.
74 This method overrides the default dispatch mechanism to the re-dispatching
75 mechanism described above.
83 Catalyst::RequestRole::REST->meta->apply($c->request)
84 unless does_role($c->request, 'Catalyst::RequestRole::REST');
86 my $controller = $c->component( $self->class );
87 my $rest_method = $self->name . "_" . uc( $c->request->method );
91 # Common case, for foo_GET etc
92 if ($code = $controller->can($rest_method)) {
93 # Exceute normal action
94 $c->execute( $self->class, $self, @{ $c->req->args } );
98 # Generic handling for foo_OPTIONS
99 if (!$code && $c->request->method eq "OPTIONS") {
100 $name = $rest_method;
101 $code = sub { $self->_return_options($self->name, @_) };
104 # Otherwise, not implemented.
106 $name = $self->name . "_not_implemented";
107 $code = $controller->can($name) # User method
108 # Generic not implemented
109 || sub { $self->_return_not_implemented($self->name, @_) };
112 # localise stuff so we can dispatch the action 'as normal, but get
113 # different stats shown, and different code run.
114 local $self->{code} = $code;
115 local $self->{reverse} = $name;
117 $c->execute( $self->class, $self, @{ $c->req->args } );
120 sub _get_allowed_methods {
121 my ( $self, $controller, $c, $name ) = @_;
122 my $class = ref($controller) ? ref($controller) : $controller;
123 my $methods = Class::Inspector->methods($class);
125 foreach my $method ( @{$methods} ) {
126 if ( $method =~ /^$name\_(.+)$/ ) {
127 push( @allowed, $1 );
133 sub _return_options {
134 my ( $self, $method_name, $controller, $c) = @_;
135 my @allowed = $self->_get_allowed_methods($controller, $c, $method_name);
136 $c->response->content_type('text/plain');
137 $c->response->status(200);
138 $c->response->header( 'Allow' => \@allowed );
141 sub _return_not_implemented {
142 my ( $self, $method_name, $controller, $c ) = @_;
144 my @allowed = $self->_get_allowed_methods($controller, $c, $method_name);
145 $c->response->content_type('text/plain');
146 $c->response->status(405);
147 $c->response->header( 'Allow' => \@allowed );
148 $c->response->body( "Method "
149 . $c->request->method
150 . " not implemented for "
151 . $c->uri_for( $method_name ) );
160 You likely want to look at L<Catalyst::Controller::REST>, which implements
161 a sensible set of defaults for a controller doing REST.
163 L<Catalyst::Action::Serialize>, L<Catalyst::Action::Deserialize>
165 =head1 TROUBLESHOOTING
169 =item Q: I'm getting a "415 Unsupported Media Type" error. What gives?!
171 A: Most likely, you haven't set Content-type equal to "application/json", or
172 one of the accepted return formats. You can do this by setting it in your query
173 accepted return formats. You can do this by setting it in your query string
174 thusly: C<< ?content-type=application%2Fjson (where %2F == / uri escaped). >>
176 B<NOTE> Apache will refuse %2F unless configured otherise.
177 Make sure C<< AllowEncodedSlashes On >> is in your httpd.conf file in orde
178 for this to run smoothly.
190 Daisuke Maki <daisuke@endeworks.jp>
192 J. Shirley <jshirley@gmail.com>
196 Tomas Doran (t0m) <bobtfish@bobtfish.net>
200 Adam Jacob <adam@stalecoffee.org>, with lots of help from mst and jrockway
202 Marchex, Inc. paid me while I developed this module. (L<http://www.marchex.com>)
206 You may distribute this code under the same terms as Perl itself.