Unable to set Guzzle content type

I am trying to request a method like this:

$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 GuzzleHttp\Client([
'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 set without a Content-Type. What am I doing wrong?

+4
source share
2 answers

Well, the problem was that I set the body and headers out of bounds. decision:

$client = new GuzzleHttp\Client([
'base_url' => [$url, []],
'defaults' => [
     'auth' => [$publishable_key, ''],
     'headers'  => ['content-type' => 'application/json', 'Accept' => 'application/json'],
     'body' => json_encode($body),
],
]);
+7
source

Guzl 6

Guzzle will set the Content-Type application/x-www-form-urlencodedheader to when the Content-Type header is missing is already present.

You have 2 options.

Option 1: On the Client directly

$client = new GuzzleHttp\Client(
    ['headers' => [
        'Content-Type' => 'application/json'
        ]
    ]
);

Option 2: On a Per Request basis

// Set various headers on a request
$client = new GuzzleHttp\Client();

$client->request('GET', '/whatever', [
    'headers' => [
        'Content-Type' => 'application/json'
    ]
]);

You can refer to Guzzle 6: Query Parameters

+2
source

All Articles