Berny
Berny

Reputation: 121

PATCH call returning "The entity is not a well-formed 'application/json-patch+json' document"

Today I was testing PATCH calls to an API using PHP for the first time and to this moment I have not quite figured how to make them work.

I want to add a new parameter with it's value to an already existing set of parameters, I have set CURLOPT_CUSTOMREQUEST => "PATCH" as option and "Content-Type: application/json-patch+json" as the proper header. Despite my body being an array as the error first indicated when I parsed it into JSON, now it says that the entity is still not a well-formed application/json-patch+json.

I am totally lost as to why this could be, since other examples I have seen do not differ from what I wrote.

This is the existing set of parameters:

{
  "ac": "774",
  "allowed_clis": [
  "34774554114"
            ],
  "cc": "34",
  "cli": "34774554114",
  "id": 1512,
  "subscriber_id": 1512
}

What I am trying to achieve:

{
  "ac": "774",
  "allowed_clis": [
  "34774554114"
            ],
  "cc": "34",
  "cli": "34774554114",
  "id": 1512,
  "e164_to_ruri": true,
  "subscriber_id": 1512
}

This is my code:

$dataArray = [
    'op' => 'add', 
    'path' => '/e164_to_ruri', 
    'value' => true
];

function setPreferences($dataArray, $uri, $token){  
    $ch= curl_init();

    $options = array(
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_SSL_VERIFYHOST => false,
        CURLOPT_SSL_VERIFYPEER => false,
        CURLOPT_CUSTOMREQUEST => "PATCH",
        CURLOPT_POSTFIELDS => $dataArray,
        CURLOPT_URL => $uri,
        CURLOPT_HTTPHEADER => array("Content-Type: application/json-patch+json", "Authorization: Basic ".$token)
    );
    curl_setopt_array($ch, $options);

    $response = curl_exec($ch);

    $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);

    return $response;
}

$response = setPreferences($dataArray, $uri, $token);
$response = json_decode($response, true);
print_r($response);

And finally, the response I get:

Array
(
    [message] => The entity is not a well-formed 'application/json-patch+json' document. Malformed JSON: Expected array or object at line 0, offset 0
    [code] => 400
)

To my limited knoledge, $dataArray is a well-formed array. So I don't understand where is this response coming from. Thank you so much for the help provided!

Upvotes: 1

Views: 258

Answers (1)

Berny
Berny

Reputation: 121

As @UlrichEckhardt pointed out, all I needed to do is turn my data into a JSON array of JSON objects:

function setPreferences($uri, $token){  
    $ch= curl_init();

    $options = array(
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_SSL_VERIFYHOST => false,
        CURLOPT_SSL_VERIFYPEER => false,
        CURLOPT_CUSTOMREQUEST => "PATCH",
        CURLOPT_POSTFIELDS => '[{"op": "add", "path": "/e164_to_ruri", "value":true}]',
        CURLOPT_URL => $uri,
        CURLOPT_HTTPHEADER => array("Content-Type: application/json-patch+json", "Authorization: Basic ".$token)
    );
    curl_setopt_array($ch, $options);

    $response = curl_exec($ch);

    $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);

    return $response;
}

$response = setPreferences($dataArray, $uri, $token);
$response = json_decode($response, true);
print_r($response);

Upvotes: 2

Related Questions