in reply to json Array with URI

Thank you for pointing me to incomplete code. I updated my post with some more code (stripping it down from my code base). However, I posted the $response->content; held in variable $json_text. (I thought this is the most important part to understand why decode_json fires an error. But from your answers, I start to think the problem must be some where else. (disclaimer: I am not a json expert, just starting using it - quite successfully - a couple of weeks ago)

Replies are listed 'Best First'.
Re^2: json Array with URI
by Corion (Patriarch) on Dec 27, 2018 at 15:14 UTC

    In your server code you have the following line:

    print "Content-type: application/json;\n\n";

    What is that line doing there and are you certain that the rest of the script hasn't already sent complete headers? This would easily explain why $response->decoded_content starts with Content-type:.

    Update: And indeed, in your server code, you have print $q->header;. Printing more headers after that will not add those lines to the headers but to the reply body. See the ->content_type method of CGI.

    This also makes your diagnosis of

    My response $json_text (an array with a list of URI) looks like this:
    somewhat suspect because there must (or rather, should) be a line Content-type: application/json; right before that.

      Now I see. You are right. I was printing twice the header. I know it was something stupid somewhere. Thank you very much for opening my eyes.

Re^2: json Array with URI
by haukex (Archbishop) on Dec 27, 2018 at 15:25 UTC
    However, I posted the $response->content; held in variable $json_text. (I thought this is the most important part to understand why decode_json fires an error.

    As Corion and I already hinted at, it's pretty unlikely that you posted exactly what $json_text contains. Please use a module such as Data::Dumper or Data::Dump.

    To output a CGI header with a different Content-Type, you can say print $q->header("application/json");

      Thank you for this hint. I posted what a simple print gave me. print Dumper is of course more complete. Learnt a lot, as always.

        I posted what a simple print gave me.

        A print would've shown the extra Content-type: ... at the beginning too, so I'm guessing maybe you missed it when you copied & pasted into the post. The modules do make the delimiters a little more clear.