Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Guzzle - How to set json Header?

Tags:

php

guzzle

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?

like image 635
I'll-Be-Back Avatar asked Nov 21 '25 08:11

I'll-Be-Back


1 Answers

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']]);
like image 173
chelmertz Avatar answered Nov 22 '25 22:11

chelmertz