1 package Catalyst::Action::Deserialize;
4 use namespace::autoclean;
6 extends 'Catalyst::Action::SerializeBase';
7 use Module::Pluggable::Object;
9 use Moose::Util::TypeConstraints;
11 our $VERSION = '0.94';
12 $VERSION = eval $VERSION;
14 has plugins => ( is => 'rw' );
16 has deserialize_http_methods => (
19 my $tc = subtype as 'HashRef[Str]';
20 coerce $tc, from 'ArrayRef[Str]',
21 via { +{ map { ($_ => 1) } @$_ } };
25 builder => '_build_deserialize_http_methods',
27 deserialize_http_methods => 'keys',
28 _deserialize_handles_http_method => 'exists',
32 sub _build_deserialize_http_methods { [qw(POST PUT OPTIONS DELETE)] }
36 my ( $controller, $c ) = @_;
38 if ( !defined($c->req->data) && $self->_deserialize_handles_http_method($c->request->method) ) {
39 my ( $sclass, $sarg, $content_type ) =
40 $self->_load_content_plugins( 'Catalyst::Action::Deserialize',
42 return 1 unless defined($sclass);
44 if ( defined($sarg) ) {
45 $rc = $sclass->execute( $controller, $c, $sarg );
47 $rc = $sclass->execute( $controller, $c );
50 return $self->_unsupported_media_type( $c, $content_type );
51 } elsif ( $rc ne "1" ) {
52 return $self->_serialize_bad_request( $c, $content_type, $rc );
56 $self->maybe::next::method(@_);
61 __PACKAGE__->meta->make_immutable;
65 Catalyst::Action::Deserialize - Deserialize Data in a Request
69 package Foo::Controller::Bar;
72 'default' => 'text/x-yaml',
73 'stash_key' => 'rest',
75 'text/x-yaml' => 'YAML',
76 'text/x-data-dumper' => [ 'Data::Serializer', 'Data::Dumper' ],
80 sub begin :ActionClass('Deserialize') {}
84 This action will deserialize HTTP POST, PUT, OPTIONS and DELETE requests.
85 It assumes that the body of the HTTP Request is a serialized object.
86 The serializer is selected by introspecting the requests content-type
89 If you want deserialize any other HTTP method besides POST, PUT,
90 OPTIONS and DELETE you can do this by setting the
91 C<< deserialize_http_methods >> list via C<< action_args >>.
92 Just modify the config in your controller and define a list of HTTP
93 methods the deserialization should happen for:
98 deserialize_http_methods => [qw(POST PUT OPTIONS DELETE GET)]
103 See also L<Catalyst::Controller/action_args>.
105 The specifics of deserializing each content-type is implemented as
106 a plugin to L<Catalyst::Action::Deserialize>. You can see a list
107 of currently implemented plugins in L<Catalyst::Controller::REST>.
109 The results of your Deserializing will wind up in $c->req->data.
110 This is done through the magic of L<Catalyst::Request::REST>.
112 While it is common for this Action to be called globally as a
113 C<begin> method, there is nothing stopping you from using it on a
116 sub foo :Local :Action('Deserialize') {}
120 When you use this module, the request class will be changed to
121 L<Catalyst::Request::REST>.
125 You likely want to look at L<Catalyst::Controller::REST>, which implements
126 a sensible set of defaults for a controller doing REST.
128 L<Catalyst::Action::Serialize>, L<Catalyst::Action::REST>
132 See L<Catalyst::Action::REST> for authors.
136 You may distribute this code under the same terms as Perl itself.