Error using API with Post

Hello,

Trying to use API to send a record, using POST, and I got these error:

URL: http://localhost:8000/api/resource/Cidade
It’s correct, because if I run this url, I get all record from my database…

17:37:36 web.1                | 2015-12-30 17:37:36,509 New Exception collected with id: 2015-12-30 17:37:36.450397-127.0.0.1-fd7
17:37:36 web.1                |  site: webconflex.local
17:37:36 web.1                |  form: {'descricao': u'ALTO ALEGRE DOS PARECIS', 'id_conflex': u'10'}
17:37:36 web.1                |
17:37:36 web.1                | Traceback (innermost last):
17:37:36 web.1                |   File "/Users/fellipeh/Developer/frappe/apps/frappe/frappe/app.py", line 72, in application
17:37:36 web.1                |     response = frappe.api.handle()
17:37:36 web.1                |   File "/Users/fellipeh/Developer/frappe/apps/frappe/frappe/api.py", line 111, in handle
17:37:36 web.1                |     data = json.loads(frappe.local.form_dict.data)
17:37:36 web.1                |   File "/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/__init__.py", line 339, in loads
17:37:36 web.1                |     return _default_decoder.decode(s)
17:37:36 web.1                |   File "/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/decoder.py", line 364, in decode
17:37:36 web.1                |     obj, end = self.raw_decode(s, idx=_w(s, 0).end())
17:37:36 web.1                |  TypeError: expected string or buffer

Using this:

JSON:

{"data":[
    {"descricao":"ALTO ALEGRE DOS PARECIS","id_conflex":10}
    ]
}

Debugging I saw this:

The json value isn’t in correct position? I think the correct is on form_dict.data not in form_dict

Try checking with a standard DocType like ToDo

@fellipeh
I hope this will help you

data = {
   "descricao": "ALTO ALEGRE DOS PARECIS",
   "id_conflex": 10
} 

url = "http://localhost:8000/api/resource/Cidade"

res = requests.post(url= url, data='data='+json.dumps(data), headers={'Content-type': 'application/json'}) 

@saurabh6790

same error here, any other idea?

@rmehta

Same error using ToDo… I use in these way:

 1 import json
  2 import requests
  3 data = {
  4        "description": "ALTO ALEGRE DOS PARECIS",
  5
  6 }
  7
  8 url = "http://localhost:8000/api/resource/ToDo"
  9
 10 res = requests.post(url= url, data='data='+json.dumps(data), headers={'Content-type': 'application/json'})

Show me these:

16:55:18 web.1                |
16:55:18 web.1                | 2016-01-04 16:55:18,382 Request Error
16:55:18 web.1                |  site: webconflex.local
16:55:18 web.1                |  form: {}
16:55:18 web.1                |
16:55:18 web.1                | Traceback (innermost last):
16:55:18 web.1                |   File "/Users/fellipeh/Developer/frappe/apps/frappe/frappe/app.py", line 72, in application
16:55:18 web.1                |     response = frappe.api.handle()
16:55:18 web.1                |   File "/Users/fellipeh/Developer/frappe/apps/frappe/frappe/api.py", line 111, in handle
16:55:18 web.1                |     data = json.loads(frappe.local.form_dict.data)
16:55:18 web.1                |   File "/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/__init__.py", line 339, in loads
16:55:18 web.1                |     return _default_decoder.decode(s)
16:55:18 web.1                |   File "/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/decoder.py", line 364, in decode
16:55:18 web.1                |     obj, end = self.raw_decode(s, idx=_w(s, 0).end())
16:55:18 web.1                |  TypeError: expected string or buffer
16:55:18 web.1                |
16:55:18 web.1                | --------------
16:55:18 web.1                |
16:55:18 web.1                | 2016-01-04 16:55:18,463 New Exception collected with id: 2016-01-04 16:55:18.382877-127.0.0.1-9fd
16:55:18 web.1                |  site: webconflex.local
16:55:18 web.1                |  form: {}
16:55:18 web.1                |
16:55:18 web.1                | Traceback (innermost last):
16:55:18 web.1                |   File "/Users/fellipeh/Developer/frappe/apps/frappe/frappe/app.py", line 72, in application
16:55:18 web.1                |     response = frappe.api.handle()
16:55:18 web.1                |   File "/Users/fellipeh/Developer/frappe/apps/frappe/frappe/api.py", line 111, in handle
16:55:18 web.1                |     data = json.loads(frappe.local.form_dict.data)
16:55:18 web.1                |   File "/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/__init__.py", line 339, in loads
16:55:18 web.1                |     return _default_decoder.decode(s)
16:55:18 web.1                |   File "/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/decoder.py", line 364, in decode
16:55:18 web.1                |     obj, end = self.raw_decode(s, idx=_w(s, 0).end())
16:55:18 web.1                |  TypeError: expected string or buffer
16:55:18 web.1                |
16:55:18 web.1                | --------------
16:55:18 web.1                | 127.0.0.1 - - [04/Jan/2016 16:55:18] "POST /api/resource/ToDo HTTP/1.1" 500 -

@fellipeh use this:

data = {
   "descricao": "ALTO ALEGRE DOS PARECIS",
   "id_conflex": 10
} 

url = "http://localhost:8000/api/resource/Cidade"

res = requests.post(url= url, data={"data": json.dumps(data)}, headers={'Content-type': 'application/json'})

Same error.

I try using frappe_client, and works!

But if I try to using Delphi, or other POST app, like postman… doesn’t work.

I need to work with delphi, any tips?

I found a solution.

I need to pass data, as Parameter…

on Delphi use this:

RestRequest.AddParameter('data', JSON);

Do not use RestRequest.Params!!!

Cheers

1 Like