Cherry pick everything bar the use parent change from 25d49c2, fixing RT#46680
[catagits/Catalyst-Action-REST.git] / lib / Catalyst / Action / Deserialize / XML / Simple.pm
1 package Catalyst::Action::Deserialize::XML::Simple;
2
3 use strict;
4 use warnings;
5
6 use base 'Catalyst::Action';
7
8 sub execute {
9     my $self = shift;
10     my ( $controller, $c, $test ) = @_;
11
12     eval {
13         require XML::Simple;
14     };
15     if ($@) {
16         $c->log->debug("Could not load XML::Simple, refusing to deserialize: $@")
17             if $c->debug;
18         return 0;
19     }
20
21     my $body = $c->request->body;
22     if ($body) {
23         my $xs = XML::Simple->new('ForceArray' => 0,);
24         my $rdata;
25         eval {
26             $rdata = $xs->XMLin( "$body" );
27         };
28         if ($@) {
29             return $@;
30         }
31         if (exists($rdata->{'data'})) {
32             $c->request->data($rdata->{'data'});
33         } else {
34             $c->request->data($rdata);
35         }
36     } else {
37         $c->log->debug(
38             'I would have deserialized, but there was nothing in the body!')
39                 if $c->debug;
40     }
41     return 1;
42 }
43
44 1;