Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: Getting error while decoding JSON object via POST method

by Mr. Muskrat (Canon)
on Feb 07, 2013 at 20:00 UTC ( #1017720=note: print w/replies, xml ) Need Help??


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

my $perlData = $json->decode($response->content());

You don't want the raw content returned by $response->content(). You want the decoded content as returned by $response->decoded_content(). tobyink had it correct in his reply but he didn't explicitly call it out and you didn't pick up on it.

Replies are listed 'Best First'.
Re^2: Getting error while decoding JSON object via POST method
by johndeighan (Novice) on Apr 05, 2017 at 12:33 UTC
    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.

      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.

      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.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1017720]
help
Chatterbox?
[erix]: heh

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (8)
As of 2017-11-22 08:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    In order to be able to say "I know Perl", you must have:













    Results (316 votes). Check out past polls.

    Notices?