I'll-Be-Back I'll-Be-Back - 1 month ago 9
PHP Question

Guzzle - How to set json Header?

It appear

Accept: application/json
has not been set for the request header. I am not getting json response.

$params = [
'client_id' => 'xxxx',
'client_secret' => 'xxxxxxxxxx',
'code' => $request->get('code'),
'state' => $request->get('state'),
];

$client = new Client(['headers' => ['Accept: application/json']]);
$response = $client->post($tokenUrl, [
'form_params' => $params,
]);

echo $response->getBody();


How do I solve this?

Answer

You should write the headers as an associative array, according to http://docs.guzzlephp.org/en/latest/request-options.html.

So, try

$client = new Client(['headers' => ['Accept' => 'application/json']]);