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;
14 use Catalyst::Request::REST;
16 __PACKAGE__->mk_accessors(qw(plugins));
20 my ( $controller, $c ) = @_;
22 my $nreq = bless( $c->request, 'Catalyst::Request::REST' );
25 my @demethods = qw(POST PUT OPTIONS);
26 my $method = $c->request->method;
27 if ( grep /^$method$/, @demethods ) {
28 my ($sclass, $sarg, $content_type) = $self->_load_content_plugins('Catalyst::Action::Deserialize', $controller, $c);
29 return 1 unless defined ($sclass);
31 if ( defined($sarg) ) {
32 $rc = $sclass->execute( $controller, $c, $sarg );
34 $rc = $sclass->execute( $controller, $c );
37 return $self->_unsupported_media_type($c, $content_type);
38 } elsif ($rc ne "1") {
39 return $self->_serialize_bad_request($c, $content_type, $rc);
43 $self->NEXT::execute( @_ );
50 Catalyst::Action::Deserialize - Deserialize Data in a Request
54 package Foo::Controller::Bar;
59 'stash_key' => 'rest',
61 'text/x-yaml' => 'YAML',
62 'text/x-data-dumper' => [ 'Data::Serializer', 'Data::Dumper' ],
67 sub begin :ActionClass('Deserialize') {}
71 This action will deserialize HTTP POST, PUT, and OPTIONS requests.
72 It assumes that the body of the HTTP Request is a serialized object.
73 The serializer is selected by introspecting the requests content-type
76 It requires that your Catalyst controller have a "serialize" entry
77 in it's configuration. See L<Catalyst::Action::Serialize> for the details.
79 The specifics of deserializing each content-type is implemented as
80 a plugin to L<Catalyst::Action::Deserialize>. You can see a list
81 of currently implemented plugins in L<Catalyst::Controller::REST>.
83 The results of your Deserializing will wind up in $c->req->data.
84 This is done through the magic of L<Catalyst::Request::REST>.
86 While it is common for this Action to be called globally as a
87 C<begin> method, there is nothing stopping you from using it on a
90 sub foo :Local :Action('Deserialize') {}
96 You likely want to look at L<Catalyst::Controller::REST>, which implements
97 a sensible set of defaults for a controller doing REST.
99 L<Catalyst::Action::Serialize>, L<Catalyst::Action::REST>
103 Adam Jacob <adam@stalecoffee.org>, with lots of help from mst and jrockway
105 Marchex, Inc. paid me while I developed this module. (http://www.marchex.com)
109 You may distribute this code under the same terms as Perl itself.