Help in REST API Script

Hi All…

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!

According to @alec_ruizramon1 solution
he means you have to do the following in PHP:

$object = 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”,
);

$result = $client->insert(“Employee” , json_encode($object) );

@mostafa hmmm, sadly the error is the same after the modifications…

@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.

Have the same FrappeClass and has something different:

public function insert($doctype, $params) {
            $this->_auth_check ();      
            var_dump ( $params );
            return $this->_curl ( 'INSERT', array (
                    'doctype' => $doctype,
                    'data' => $params 
            ) );
        }

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.

I am looking same GitHub - tmimori/How-to-connect-to-ERPNext-v5-API-with-PHP5: How to connect to ERPNext v5 API with PHP 5
please explain how to set authentication in config.php file & which url set

‘auth_url’ => ‘http://frappe.local/api/method/login’,
‘api_url’ => ‘http://frappe.local/api/resource/’,

NOT working

Try to put your server adresse insted of frappe.local

i done same but i got error “Auth fail”
‘auth_url’ => ‘Frappe Cloud’,
‘api_url’ => ‘Frappe Cloud’,
‘auth’ => array(‘usr’ => ‘amolvision@outlook.com’, ‘pwd’ => ‘#######’),
‘cookie_file’ => ‘cookie.txt’,
‘curl_timeout’ => 30,

'basic_auth' => array(),

Authentication done but i got 500 error for insertion operation
update/delete/search working

ISSUE in INSERT FOR ALL MODULES

Server Error

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

---------CURL INFO -------------------

"
[“header”]=>
array(26) {
[“url”]=>
string(50) “Frappe Cloud
[“content_type”]=>
string(24) “text/html; charset=utf-8”
[“http_code”]=>
int(500)
[“header_size”]=>
int(529)
[“request_size”]=>
int(306)
[“filetime”]=>
int(-1)
[“ssl_verify_result”]=>
int(0)
[“redirect_count”]=>
int(0)
[“total_time”]=>
float(0.802711)
[“namelookup_time”]=>
float(4.6E-5)
[“connect_time”]=>
float(0.149229)
[“pretransfer_time”]=>
float(0.461345)
[“size_upload”]=>
float(0)
[“size_download”]=>
float(7282)
[“speed_download”]=>
float(9071)
[“speed_upload”]=>
float(0)
[“download_content_length”]=>
float(7282)
[“upload_content_length”]=>
float(-1)
[“starttransfer_time”]=>
float(0.802617)
[“redirect_time”]=>
float(0)
[“certinfo”]=>
array(0) {
}
[“primary_ip”]=>
string(15) “195.154.133.212”
[“primary_port”]=>
int(443)
[“local_ip”]=>
string(13) “198.15.114.14”
[“local_port”]=>
int(59990)
[“redirect_url”]=>
string(0) “”
}
[“is_auth”]=>
bool(true)
[“_auth_url”:“FrappeClient”:private]=>
string(43) “Frappe Cloud
[“_api_url”:“FrappeClient”:private]=>
string(40) “Frappe Cloud
[“_cookie_file”:“FrappeClient”:private]=>
string(10) “cookie.txt”
[“_auth”:“FrappeClient”:private]=>
array(2) {
[“usr”]=>
string(22) “amolvision@outlook.com
[“pwd”]=>
string(10) “q1w2e3r4t5”
}
[“_basic_auth”:“FrappeClient”:private]=>
array(0) {
}
[“_curl_timeout”:“FrappeClient”:private]=>
int(30)
[“_limit_page_length”:“FrappeClient”:private]=>
int(20)
}

Is this lib work for all erpnext modules

Getting Same issue.

No JSON object could be decoded

ERPNext: v7.2.6
Frappe Framework: v7.2.5

Use below code in FrappeClient.php file.

curl_setopt( $ch, CURLOPT_HEADER, true);
curl_setopt( $ch, CURLOPT_HTTPHEADER,
array(‘Content-Type:application/json’,
'Content-Length: ’ . strlen( $params[‘data’] ) )
);

1 Like

@Sandip_Dalvi Yes, you are correct. After use this, i am able to create Item with Rest api.

try this

$data = json_encode($params[‘data’]);
$length = strlen($data);

curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt( $ch, CURLOPT_HEADER, true);
curl_setopt( $ch, CURLOPT_HTTPHEADER, array(
‘Content-Type: application/json’,
'Content-Length: ’ . $length));