I'll-Be-Back I'll-Be-Back - 1 year ago 130
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 Source

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']]);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download