Skip to content
Advertisement

Can’t set Guzzle Content Type

I’m trying to request this way:

$body = [];
$body['holder_name'] = $full_name;
$body['bank_code'] = $bank_number;
$body['routing_number'] = $branch_number;
$body['account_number'] = $account_number;
$body['type'] = 'checking';

$client = new GuzzleHttpClient([
'base_url' => [$url, []],
'headers'  => ['content-type' => 'application/json', 'Accept' => 'application/json'],
    'defaults' => [
         'auth' => [$publishable_key, ''],
],
    'body' => json_encode($body),
]);

The problem is that this request is being set without Content-Type. What am I doing wrong?

Advertisement

Answer

Ok .. the problem was that I was setting body and headers outside of defautls. the solution is:

$client = new GuzzleHttpClient([
'base_url' => [$url, []],
'defaults' => [
     'auth' => [$publishable_key, ''],
     'headers'  => ['content-type' => 'application/json', 'Accept' => 'application/json'],
     'body' => json_encode($body),
],
]);
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement