Short:
We get the following error message when sending valid JSON with a Character Encoding other than UTF-8:
{
“status”: false,
“error”: “Invalid or malformed JSON: control character error, possibly incorrectly encoded”
}
Long:
The current JSON Spec explicitly allows for Character Encoding other than UTF-8. This is stated in rfc4627, section-3. JSON Spec: Encoding. It states that:
JSON text SHALL be encoded in Unicode. The default encoding is
UTF-8.
Since the first two characters of a JSON text will always be ASCII
characters [RFC0020], it is possible to determine whether an octet
stream is UTF-8, UTF-16 (BE or LE), or UTF-32 (BE or LE) by looking
at the pattern of nulls in the first four octets.
This is currently not the case with the Pipedrive API, which does not accept any other encoding then UTF-8. (To be honest, I only tested with “activities”, but assume this applies to all).
How to Reproduce
Here is a script, on how to reproduce the issue. The “Super Secret” Elements obviously will have to be replaced…
<?php
// Pipedrive API token
$api_token = '<Super Secret>';
// Pipedrive company domain
$company_domain = '<Super Secret>';
$deal_id = <Super Secret>;
// Payload
$data = array(
'subject' => 'Test Encoding',
'type' => 'lunch',
'deal_id' => $deal_id,
"public_description" => "This is a test description",
'note' => "This is a test note"
);
// URL for adding an Activity
$url = 'https://' . $company_domain . '.pipedrive.com/api/v1/activities?api_token=' . $api_token;
// UTF-16BE (Example. Could be anything like -16xx, -32xx)
$data = mb_convert_encoding(json_encode($data), 'UTF-16BE');
$payload = $data;
//
$ch = curl_init();
curl_setopt_array($ch, [
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS =>$payload,
CURLOPT_HTTPHEADER => [
'Content-Type: application/json'
],
]);
$output = curl_exec($ch);
print_r(curl_getinfo($ch));
print_r($output);
curl_close($ch);
Question:
Is there any hope, that this will be fixed - or better: other encoding will be supported - anytime soon?