Keith KMSPIC
Keith KMSPIC

Reputation: 53

Zoho API V2 Update Record

I am trying to use the Zoho API Version 2 to do a simple record update in Leads. I am using PHP and CURL and my sample code for this call (to update a single field in the record) is as follows:-

$apiUrl = "https://www.zohoapis.com/crm/v2/Leads/" . {valid record id here};

$headers = array(
    'Content-Type: application/json',
    'Content-Length: ' . strlen($fields),
    sprintf('Authorization: Zoho-oauthtoken %s', {valid auth token here})
);

$fields = json_encode([["data" => ["City" => "Egham"]]]);

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $apiUrl);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 60); 
curl_setopt($ch, CURLOPT_TIMEOUT, 60); 

$result = curl_exec($ch);

curl_close($ch); 

No matter how I format the JSON, I always get the following returned:

{"code":"INVALID_DATA","details": {"expected_data_type":"jsonobject"},"message":"body","status":"error"} 

It is not a matter of invalid auth tokens etc because I have successfully used PHP and CURL with the Zoho API to read data and I decode the JSON returned successfully.

Please could somebody help with passing valid JSON data?

Upvotes: 4

Views: 4526

Answers (2)

HoangYell
HoangYell

Reputation: 4669

You must escape the data into JSON format:

import json
response = requests.post(url, data=json.dumps(data), headers=headers)

Upvotes: 0

Sumanth Reddy Chilka
Sumanth Reddy Chilka

Reputation: 96

The above code constructs input JSON like this [{"data":{"City":"Egham"}}]. This JSON is not valid as per the ZOHO CRM APIs(API help).

It should be like this {"data":[{"City":"Egham"}]}.

Change the code like this :

$apiUrl = "https://www.zohoapis.com/crm/v2/Leads/" . {valid record id here};

$fields = json_encode(array("data" => array(["City" => "Egham"])));

// *strlen must be called after defining the variable. So moved headers down to $fields*

$headers = array(
    'Content-Type: application/json',
    'Content-Length: ' . strlen($fields),
    sprintf('Authorization: Zoho-oauthtoken %s', {valid auth token here})
);


$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $apiUrl);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 60);
curl_setopt($ch, CURLOPT_TIMEOUT, 60);

$result = curl_exec($ch);

curl_close($ch); 

Upvotes: 8

Related Questions