Update child table using REST

I have 2 Doctypes “Cart” and “Cart Items”. Cart Items is a child table of Cart.
I want to add items to Cart Items using REST API but it return code 500

No JSON object could be decoded

I can successfully GET and DELETE until now. But not update

This is the data I am Sending:

http://MYURL.com/api/resource/Cart/testCart

Where testCart is the name of the Cart Doctype

and I pass as parameters:

data = {
“items”: [{
“item_code”: ‘DES 3453’,
“amount”: 3
}]
}

Below I have attached the photos of Cart and Cart Item
Cart:

Cart Item

Any idea?

@revant_one any help please?

I think you may need to pass the ‘name’ parameter for Cart as well as the name.
Also, what type of method are you using to pass this? POST? PUT?

I am following the guidelines here
https://frappe.io/docs/user/en/guides/integration/rest_api

To update I must use PUT. (I tried with POST but it does not work)

The name I pass it on URL.

http://MYURL.com/api/resource/Cart/testCart

It is ‘testcart’

That is how it is mentioned in the documentation

1 Like

Try getting the testCart document using GET and then just change the fields you want to change. There is much more information in the document than the fields you are updating (such as child table entry names, etc).

data = {
“items”: [{
“item_code”: ‘DES 3453’,
“amount”: 3
}]
}

replace with this

data = {
“items”: [{
“item_code”: “DES 3453”,
“amount”: 3
}]
}

names are generated automatically i presume and other stuffs. I only have 2 fields there

there must be one mandatory or optional parent table field.

please send that as well.

For Example:

data = {
“CartNo”:“Test”,
“items”: [{
“item_code”: “DES 3453”,
“amount”: 3
}]
}

Can you show how you are sending the data exactly ? Can’t really seem to open the link you shared in your post.

It does not work like this.

Because I have changed my url. I am not going to show my real URL.

It still does not work.

I tried with the same code to POST, GET, and DELTE and it works.

Can you share the GET results?

ON GET:

Reponse 200
And it returns the Cart Data

{“data”:[{“customer”:“City Generators LLC”,“_assign”:null,“modified_by”:“Administrator”,“name”:“City Generators LLC”,“parent”:null,“_user_tags”:null,“creation”:“2017-09-11 15:49:30.985709”,“modified”:“2017-09-15 11:35:39.799639”,“cus”:null,“idx”:0,“parenttype”:null,“_liked_by”:null,“owner”:“Administrator”,“docstatus”:0,“_comments”:null,“parentfield”:null}]}

What content type should I use? I am using

‘Content-Type’: ‘application/json’
I don’t know if it is right or not.

@revant_one I’m sorry to bother you again but do you have any idea why this might happen? Is this frappe bug or mine? I can not seem to be able to put to work POST and PUT. While GET and DELETE works perfectly.

Can You share the Error Log for PUT and POST Request.

that will help to identify the issue.

@adnan

‘INTERNAL SERVER ERROR’

Traceback (most recent call last):
File “/home/frappe/frappe-bench/apps/frappe/frappe/app.py”, line 62, in application
response = frappe.api.handle()
File “/home/frappe/frappe-bench/apps/frappe/frappe/api.py”, line 82, in handle
data = json.loads(frappe.local.form_dict.data)
File “/usr/lib/python2.7/json/init.py”, line 339, in loads
return _default_decoder.decode(s)
File “/usr/lib/python2.7/json/decoder.py”, line 364, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File “/usr/lib/python2.7/json/decoder.py”, line 382, in raw_decode
raise ValueError(“No JSON object could be decoded”)
ValueError: No JSON object could be decoded

Status: 500

This is the code:

session = OAuth2Session(client_id=global_vars.client_id, client_secret=global_vars.client_sercret,
access_token=decode(global_vars.encryption_key, request.session[‘token’]))

data = {
“items”: [
{
“item_code”: “DON P551422”
}]
}

response = session.put(‘http://[URL]/api/resource/Cart/City Generators LLC’, params=data)

P.S. I removed the real url. I have provided other informations in my first post.

Can you try using POSTMAN or something to send your requests? It could be in how you are sending the data.