[solved] withJson($array) returns null


#1

Hi, I am returning a simple array:

return ['ABC' => 'DEF'];

which passes through the CoreFunctions, more explicitly $response->withJson($resp) .

I am expecting a JSON object returned in the format:

{
   "ABC": "DEF"
}

But I get nothing back (and the response header is set to text/html; charset=UTF-8) .

However, if I run

return json_encode(['ABC' => 'DEF']);

then I get the expected outcome.

I thought ->withJson would do the above, or is this expected behaviour? (I’m running Slim 3.9.2)


#2

I test your case, I get the expected JSON doing:

 $app->get('/test/', function (Request $request, Response $response, array $args) {
    $a = ['abc'=>'def'];
    return $response->withJson($a);
});

the header is:

Content-Type:application/json;charset=utf-8


#3

Thank you @mzc, with your help I managed to pinpoint the issue. I was pushing all requests through a route resolver, and that’s what was causing the unexpected behaviour.

All sorted now, thanks again :smile:


#4

No problem! :slightly_smiling_face: