Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 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 being set without Content-Type. What am I doing wrong?

like image 514
Bug Avatar asked Jan 29 '15 18:01

Bug


People also ask

What are the default settings for the guzzlehttp client?

Apparently there is no "defaults" settings in GuzzleHTTP 6.2 anymore, every setting that's passed when creating the client becomes a default for this client instance. Guzzle will set the Content-Type header to application/x-www-form-urlencoded when no Content-Type header is already present.

How to decode content-encoding responses in guzzle?

Specify whether or not Content-Encoding responses (gzip, deflate, etc.) are automatically decoded. true GuzzleHttp\RequestOptions::DECODE_CONTENT This option can be used to control how content-encoded response bodies are handled. By default, decode_content is set to true, meaning any gzipped or deflated response will be decoded by Guzzle.

What is the use of guzzle?

Guzzle Documentation ¶ Guzzle is a PHP HTTP client that makes it easy to send HTTP requests and trivial to integrate with web services. Simple interface for building query strings, POST requests, streaming large uploads, streaming large downloads, using HTTP cookies, uploading JSON data, etc...

What is the body option in guzzle http?

This is currently only supported when using the cURL handler. The body option is used to control the body of an entity enclosing request (e.g., PUT, POST, PATCH). None GuzzleHttp\RequestOptions::BODY This setting can be set to any of the following types: This option cannot be used with form_params, multipart, or json


2 Answers

Guzzle 6

Guzzle will set the Content-Type header to application/x-www-form-urlencoded when no Content-Type header 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: Request Options

like image 160
Mick Avatar answered Oct 02 '22 12:10

Mick


I was encountering the same issue with the Hubspot API that requires to set application/json as Content-Type for POST requests.

I fixed it this way

$client = new Client([
    'base_uri' => 'https://api.hubapi.com/',
    'timeout'  => 5,
    'headers' => ['Content-Type' => 'application/json']
]);

And then performing my requests the regular way

try
{
    $response = $client->request('POST', '/contacts/v1/contact/email/[email protected]/profile',
    ['query' => MY_HUBSPOT_API_KEY, 'body' => $body]);
}
catch (RequestException $e) { print_r($e); }

I hope this helps.

like image 36
Bruno Leveque Avatar answered Oct 02 '22 13:10

Bruno Leveque