in reply to Re: Getting error while decoding JSON object via POST method
in thread Getting error while decoding JSON object via POST method

I know that this is an old thread, but I'm having exactly this problem and the answer presented isn't entirely satisfactory. What I mean is that the problem presented used the CGI module on the server side, but the solution presented (using $request->decoded_content instead of $request->content) applies to the client script, which uses LWP, but not to the server script. I don't see anything in the CGI module that will do the required decoding. In my case, I'm using Apache and an Apache::Request object, which also does not (from what I can find on the web) chunked data. I have no control over the format of the data arriving at my server. Most of the time my script works, presumably because it's usually not chunked. I am using Fiddler to debug my script. When I first inspect the response, the POST body appears to have stray characters before and after the actual JSON code. However, Fiddler tells me that I need to decode it, which I do by simply clicking a button, after which the JSON appears correct. This leads me to believe that I'm having exactly the problem described in this thread. If I had an HTTP::Request object, I would just call $req->decoded_content() but the only way that I know to get one is using LWP, which doesn't make sense on the server side.
  • Comment on Re^2: Getting error while decoding JSON object via POST method

Replies are listed 'Best First'.
Re^3: Getting error while decoding JSON object via POST method
by Corion (Patriarch) on Apr 05, 2017 at 12:37 UTC

    So, can you show us the code you are using, together with "good" and "bad" input data? Ideally, the code would be about 20 lines.

Re^3: Getting error while decoding JSON object via POST method
by huck (Prior) on Apr 05, 2017 at 13:27 UTC

    http://search.cpan.org/dist/HTML-Parser/lib/HTML/Entities.pm

    decode_entities( $string, ... )

    This routine replaces HTML entities found in the $string with the corresponding Unicode character. Unrecognized entities are left alone.

    use HTML::Entities qw/decode_entities/; my $ok=decode_entities($orig);
    Im not sure this would work on "chunks", $orig has to be the entire string to decode at once for with chunks an entity could be split across them.