jfinkels / flask-restless

NO LONGER MAINTAINED - A Flask extension for creating simple ReSTful JSON APIs from SQLAlchemy models.
https://flask-restless.readthedocs.io
GNU Affero General Public License v3.0
1.02k stars 301 forks source link

[1.0.0b1] Post requests result in MissingData (Error 400) #541

Closed cryarchy closed 8 years ago

cryarchy commented 8 years ago

I have the 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 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@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 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)

NB: I get the same results when I make the POST request from Angular2 post

cryarchy commented 8 years ago

So I went digging around the file /home/user/tizy/MyFlask/e2-papers/venv/local/lib/python2.7/site-packages/flask_restless/serialization.py and added the code print(document) at line 692. The flask-debug console now prints:

...
{u'username': u'cryarchy', 'authToken': 'U08UXA5J8PD6PDU7JXWZMR4Q', 'person': <e2papers.app.Person.model.Person object at 0x7f69ca865490>, 'date': '02/06/2016 08:58:51', u'password': u'Pass1234', u'email': u'email@domain.com'}
...

as the document. The comment under the call function however states that Everything in the document other than the data element is ignored. I don't know if this is intended and if so, how do I adapt my code to this behavior?

cryarchy commented 8 years ago

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@domain.com'}, 'type': 'account'}}
jfinkels commented 8 years ago

Yes, sorry about the sudden change. Flask-Restless 1.0.0 will support only the JSON API protocol.