2 # Catlyst::Action::Deserialize
3 # Created by: Adam Jacob, Marchex, <adam@marchex.com>
7 package Catalyst::Action::Deserialize;
12 use base 'Catalyst::Action::SerializeBase';
13 use Module::Pluggable::Object;
15 __PACKAGE__->mk_accessors(qw(plugins));
19 my ( $controller, $c ) = @_;
21 my @demethods = qw(POST PUT OPTIONS);
22 my $method = $c->request->method;
23 if ( grep /^$method$/, @demethods ) {
24 my ( $sclass, $sarg, $content_type ) =
25 $self->_load_content_plugins( 'Catalyst::Action::Deserialize',
27 return 1 unless defined($sclass);
29 if ( defined($sarg) ) {
30 $rc = $sclass->execute( $controller, $c, $sarg );
32 $rc = $sclass->execute( $controller, $c );
35 return $self->_unsupported_media_type( $c, $content_type );
36 } elsif ( $rc ne "1" ) {
37 return $self->_serialize_bad_request( $c, $content_type, $rc );
41 $self->NEXT::execute(@_);
48 Catalyst::Action::Deserialize - Deserialize Data in a Request
52 package Foo::Controller::Bar;
55 'default' => 'text/x-yaml',
56 'stash_key' => 'rest',
58 'text/x-yaml' => 'YAML',
59 'text/x-data-dumper' => [ 'Data::Serializer', 'Data::Dumper' ],
63 sub begin :ActionClass('Deserialize') {}
67 This action will deserialize HTTP POST, PUT, and OPTIONS requests.
68 It assumes that the body of the HTTP Request is a serialized object.
69 The serializer is selected by introspecting the requests content-type
72 The specifics of deserializing each content-type is implemented as
73 a plugin to L<Catalyst::Action::Deserialize>. You can see a list
74 of currently implemented plugins in L<Catalyst::Controller::REST>.
76 The results of your Deserializing will wind up in $c->req->data.
77 This is done through the magic of L<Catalyst::Request::REST>.
79 While it is common for this Action to be called globally as a
80 C<begin> method, there is nothing stopping you from using it on a
83 sub foo :Local :Action('Deserialize') {}
87 When you use this module, the request class will be changed to
88 L<Catalyst::Request::REST>.
92 You likely want to look at L<Catalyst::Controller::REST>, which implements
93 a sensible set of defaults for a controller doing REST.
95 L<Catalyst::Action::Serialize>, L<Catalyst::Action::REST>
99 Adam Jacob <adam@stalecoffee.org>, with lots of help from mst and jrockway
101 Marchex, Inc. paid me while I developed this module. (http://www.marchex.com)
105 You may distribute this code under the same terms as Perl itself.