Archy
Archy

Reputation: 320

Why does my POST requests on flask-restless==1.0.0b1 result in ERROR:400 MissingData

I have the following code:

import requests
data = {u"username":u"cryarchy", u"password":u"Pass1234", u"email":u"[email protected]"}
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 the request, the flask-debug console displays the 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 protected]'}
--------------------------------------------------------------------------------
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 is getting printed on the debug window because I have the following POST preprocessor:

def post_vars_init(data=None):
    if (not data):
        raise ProcessingException(description="No data", code=500)
    print(data)

Upvotes: 0

Views: 66

Answers (1)

Archy
Archy

Reputation: 320

Noticed my mistake after a revisit to https://flask-restless.readthedocs.io/en/1.0.0b1/creating.html. The post request data format is what I'd got wrong. It should be:

data = {'data': {'attributes': {u'username': u'cryarchy', u'password': u'Pass1234', u'email': u'[email protected]'}, 'type': 'account'}}

Upvotes: 0

Related Questions