allow $c->req->body to be a simple filehandle or a string
[catagits/Catalyst-Action-REST.git] / lib / Catalyst / Action / Deserialize / JSON.pm
index 8799735..3f718bf 100644 (file)
@@ -2,6 +2,7 @@ package Catalyst::Action::Deserialize::JSON;
 
 use Moose;
 use namespace::autoclean;
+use Scalar::Util qw(openhandle);
 
 extends 'Catalyst::Action';
 use JSON;
@@ -13,12 +14,17 @@ sub execute {
     my $self = shift;
     my ( $controller, $c, $test ) = @_;
 
-    my $body = $c->request->body;
     my $rbody;
 
-    if ($body) {
-        while (my $line = <$body>) {
-            $rbody .= $line;
+    # could be a string or a FH
+    if ( my $body = $c->request->body ) {
+        if(openhandle $body) {
+            seek($body, 0, 0); # in case something has already read from it
+            while ( defined( my $line = <$body> ) ) {
+                $rbody .= $line;
+            }
+        } else {
+            $rbody = $body;
         }
     }