in reply to Re: Need same JSON response from Mojo & CGI::App
in thread Need same JSON response from Mojo & CGI::App

With that I get hash- or arrayref expected (not a simple scalar, use allow_nonref to allow this) returned

Replies are listed 'Best First'.
Re^3: Need same JSON response from Mojo & CGI::App (which?)
by LanX (Saint) on Jan 04, 2017 at 11:32 UTC
    You haven't told us which result you need on the receiving side.

    Only "hello" ?

    Since JSON is an (JS) Object Notation I'm not sure if blank strings (i.e. nonref) are legal.

    Cheers Rolf
    (addicted to the Perl Programming Language and ☆☆☆☆ :)
    Je suis Charlie!

Re^3: Need same JSON response from Mojo & CGI::App
by Anonymous Monk on Jan 04, 2017 at 11:22 UTC
    my mistake, I get the following {"Hello":1}

      It seems that CGI::Application itself doesn't declare a ->json_body method, but judging from the error message you get, you need to configure the JSON generator of that module to allow non-reference things to be output as JSON (strings).

      The error message most likely comes from JSON::XS, so $whatever->json_generator->allow_nonref() maybe is the correct method call.

      Looking at JSON CGI::Application, I see CGI::Application::Plugin::JSON, which uses JSON::Any, which doesn't really have a way to specify configuration to whatever module it instantiates. There is a hint of setting $ENV{JSON_ANY_CONFIG} in the code, which might work for you, because the plugin itself does not provide any way to give it a preconfigured JSON encoder. Bad plugin.

        SO do you suggest I manually add a json header then simply return the string "Hello"?
      How about
      $c->render( json => { 'Hello' => undef } );