1 package Catalyst::Action::Deserialize;
4 use namespace::autoclean;
6 extends 'Catalyst::Action::SerializeBase';
7 use Module::Pluggable::Object;
10 our $VERSION = '0.90';
11 $VERSION = eval $VERSION;
13 has plugins => ( is => 'rw' );
15 has deserialize_http_methods => (
17 isa => 'ArrayRef[Str]',
18 builder => '_build_deserialize_http_methods',
20 deserialize_http_methods => 'elements',
24 sub _build_deserialize_http_methods { [qw(POST PUT OPTIONS DELETE)] }
28 my ( $controller, $c ) = @_;
30 my $method = $c->request->method;
31 if ( grep /^$method$/, $self->deserialize_http_methods ) {
32 my ( $sclass, $sarg, $content_type ) =
33 $self->_load_content_plugins( 'Catalyst::Action::Deserialize',
35 return 1 unless defined($sclass);
37 if ( defined($sarg) ) {
38 $rc = $sclass->execute( $controller, $c, $sarg );
40 $rc = $sclass->execute( $controller, $c );
43 return $self->_unsupported_media_type( $c, $content_type );
44 } elsif ( $rc ne "1" ) {
45 return $self->_serialize_bad_request( $c, $content_type, $rc );
49 $self->maybe::next::method(@_);
54 __PACKAGE__->meta->make_immutable;
58 Catalyst::Action::Deserialize - Deserialize Data in a Request
62 package Foo::Controller::Bar;
65 'default' => 'text/x-yaml',
66 'stash_key' => 'rest',
68 'text/x-yaml' => 'YAML',
69 'text/x-data-dumper' => [ 'Data::Serializer', 'Data::Dumper' ],
73 sub begin :ActionClass('Deserialize') {}
77 This action will deserialize HTTP POST, PUT, OPTIONS and DELETE requests.
78 It assumes that the body of the HTTP Request is a serialized object.
79 The serializer is selected by introspecting the requests content-type
82 If you want deserialize any other HTTP method besides POST, PUT,
83 OPTIONS and DELETE you can do this by setting the
84 C<< deserialize_http_methods >> list via C<< action_args >>.
85 Just modify the config in your controller and define a list of HTTP
86 methods the deserialization should happen for:
91 deserialize_http_methods => [qw(POST PUT OPTIONS DELETE GET)]
96 See also L<Catalyst::Controller/action_args>.
98 The specifics of deserializing each content-type is implemented as
99 a plugin to L<Catalyst::Action::Deserialize>. You can see a list
100 of currently implemented plugins in L<Catalyst::Controller::REST>.
102 The results of your Deserializing will wind up in $c->req->data.
103 This is done through the magic of L<Catalyst::Request::REST>.
105 While it is common for this Action to be called globally as a
106 C<begin> method, there is nothing stopping you from using it on a
109 sub foo :Local :Action('Deserialize') {}
113 When you use this module, the request class will be changed to
114 L<Catalyst::Request::REST>.
118 You likely want to look at L<Catalyst::Controller::REST>, which implements
119 a sensible set of defaults for a controller doing REST.
121 L<Catalyst::Action::Serialize>, L<Catalyst::Action::REST>
125 See L<Catalyst::Action::REST> for authors.
129 You may distribute this code under the same terms as Perl itself.