From 635b2731837f17f9ce2301c7532c63a337e75565 Mon Sep 17 00:00:00 2001 From: David Precious Date: Thu, 14 Sep 2023 20:32:51 +0100 Subject: [PATCH] Rewind body filehandle before reading JSON If something else had already read from the filehandle, we'd get nothing, and end up throwing an error like: ``` [error] Caught exception in engine "Error Parsing POST 'undef', Error: malformed JSON string, neither tag, array, object, number, string or atom, at character offset 0 (before "(end of string)") at /home/davidp/perl5/lib/perl5/Catalyst.pm line 4092, <$fh> chunk 11." ``` ... that sounds like we got an empty POST or something, but in fact the problem was that a plugin had caused the request body to have already been read, so the filehandle wasn't at the beginning. This seek means that we will read and parse the whole body content as intended, even if something had already read from the filehandle. I think this will also likely solve #183. --- lib/Catalyst.pm | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/Catalyst.pm b/lib/Catalyst.pm index 832c7c40..c8cbc2fc 100644 --- a/lib/Catalyst.pm +++ b/lib/Catalyst.pm @@ -4088,6 +4088,7 @@ sub default_data_handlers { my $slurped; return eval { local $/; + $fh->seek(0,0); # in case it's already been read $slurped = $fh->getline; JSON::MaybeXS::decode_json($slurped); # decode_json does utf8 decoding for us } || Catalyst::Exception->throw(sprintf "Error Parsing POST '%s', Error: %s", (defined($slurped) ? $slurped : 'undef') ,$@);