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