python - Why does my POST requests on flask-restless==1.0.0b1 result in ERROR:400 MissingData -
i have following code:
import requests data = {u"username":u"cryarchy", u"password":u"pass1234", u"email":u"email@domain.com"} url = "http://localhost:5000/api/account" headers = {"content-type":"application/vnd.api+json", "accept":"application/vnd.api+json"} import json r = requests.post(url, data=json.dumps(data), headers=headers)
after request, flask-debug console displays following:
127.0.0.1 - - [02/jun/2016 08:33:03] "post /api/account http/1.1" 400 - {u'username': u'cryarchy', u'password': u'pass1234', u'email': u'email@domain.com'} -------------------------------------------------------------------------------- error in base [/home/user/tizy/myflask/e2-papers/venv/local/lib/python2.7/site-packages/flask_restless/views/base.py:726]: -------------------------------------------------------------------------------- traceback (most recent call last): file "/home/user/tizy/myflask/e2-papers/venv/local/lib/python2.7/site-packages/flask_restless/views/resources.py", line 385, in post instance = self.deserialize(data) file "/home/user/tizy/myflask/e2-papers/venv/local/lib/python2.7/site-packages/flask_restless/serialization.py", line 693, in __call__ raise missingdata missingdata 127.0.0.1 - - [02/jun/2016 08:33:11] "post /api/account http/1.1" 400 -
the data getting printed on debug window because have following post preprocessor:
def post_vars_init(data=none): if (not data): raise processingexception(description="no data", code=500) print(data)
noticed mistake after revisit https://flask-restless.readthedocs.io/en/1.0.0b1/creating.html. post request data format i'd got wrong. should be:
data = {'data': {'attributes': {u'username': u'cryarchy', u'password': u'pass1234', u'email': u'email@domain.com'}, 'type': 'account'}}