Added an optional mode for RFC 7231 compliance. The Content-Type header is used to...
[catagits/Catalyst-Action-REST.git] / lib / Catalyst / Action / Deserialize / Callback.pm
CommitLineData
178f8470 1package Catalyst::Action::Deserialize::Callback;
2
3use Moose;
4use namespace::autoclean;
5use Scalar::Util qw(openhandle);
6
7extends 'Catalyst::Action';
8
178f8470 9sub execute {
10 my $self = shift;
11 my ( $controller, $c, $callbacks ) = @_;
12
13 my $rbody;
14
15 # could be a string or a FH
16 if ( my $body = $c->request->body ) {
17 if(openhandle $body) {
18 seek($body, 0, 0); # in case something has already read from it
19 while ( defined( my $line = <$body> ) ) {
20 $rbody .= $line;
21 }
22 } else {
23 $rbody = $body;
24 }
25 }
26
27 if ( $rbody ) {
28 my $rdata = eval { $callbacks->{deserialize}->( $rbody, $controller, $c ) };
29 if ($@) {
30 return $@;
31 }
32 $c->request->data($rdata);
33 } else {
34 $c->log->debug(
35 'I would have deserialized, but there was nothing in the body!')
36 if $c->debug;
37 }
38 return 1;
39}
40
41__PACKAGE__->meta->make_immutable;
42
431;
44