i need some help in the REST API please:
i am using FrappeClient-PHP (API Wrapper)
when i login all is good, and some commands goes well (like search), but when it comes to insert it dose not work:
the API call is:
$result = $client->insert(
“Employee”
,array(
“date_of_joining” => “22-04-2016”,
“date_of_birth” => “23-04-1989”,
“employee_name” => “markssss”,
“gender” => “Male”,
“status” => “Active”,
“company” => “T Center International Co., Ltd”,
)
);
var_dump($result);
but the result give an error:
Traceback (innermost last):
File "/home/frappe/frappe-bench/apps/frappe/frappe/app.py", line 60, in application
response = frappe.api.handle()
File "/home/frappe/frappe-bench/apps/frappe/frappe/api.py", line 111, in handle
data = json.loads(frappe.local.form_dict.data)
File "/usr/lib/python2.7/json/__init__.py", line 338, in loads
return _default_decoder.decode(s)
File "/usr/lib/python2.7/json/decoder.py", line 366, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
TypeError: expected string or buffer
The API assumes what you are passing in is a string, and tries to load it into a JSON object (second line in the traceback, data = json.loads(…) ).
I don’t know PHP really at all - but try to json-ify what you are sending to the API (and check that the method in the API wrapper casts it to string) so that it can be decoded in Frappe!
@mostafa I think you want to encode it as JSON, then dump to string, then send to the API.
It seems redundant but skipping object->JSON and going directly object->string can cause parse errors down the road - again, I don’t know PHP’s behavior translating that object into a string representation of the JSON.
If I remove the var_dump, send the error you have. So maybe is that what you need.
Don’t do a JSON encode when use insert function. The variable $params is already encoded in other function at FrappeClient.php . Just follow the code.
Traceback (most recent call last):
File “/home/frappe/benches/bench-2016-12-21/apps/frappe/frappe/app.py”, line 60, in application
response = frappe.api.handle()
File “/home/frappe/benches/bench-2016-12-21/apps/frappe/frappe/api.py”, line 115, in handle
data = json.loads(frappe.local.form_dict.data)
File “/usr/lib64/python2.7/json/init.py”, line 338, in loads
return _default_decoder.decode(s)
File “/usr/lib64/python2.7/json/decoder.py”, line 365, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File “/usr/lib64/python2.7/json/decoder.py”, line 383, in raw_decode
raise ValueError(“No JSON object could be decoded”)
ValueError: No JSON object could be decoded