Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

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.

      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?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1017720]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (4)
As of 2018-02-22 05:45 GMT
Find Nodes?
    Voting Booth?
    When it is dark outside I am happiest to see ...

    Results (288 votes). Check out past polls.