为什么我对于flask-restless == 1.0.0b1的POST请求导致ERROR:400 MissingData

时间:2016-06-02 05:47:45

标签: python flask-restless

我有以下代码:

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)

请求后,flask-debug控制台显示以下内容:

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 -

数据打印在调试窗口上,因为我有以下 POST 预处理器:

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

1 个答案:

答案 0 :(得分:0)

重新访问https://flask-restless.readthedocs.io/en/1.0.0b1/creating.html后注意到我的错误。邮政请求数据格式是我出错的。它应该是:

data = {'data': {'attributes': {u'username': u'cryarchy', u'password': u'Pass1234', u'email': u'email@domain.com'}, 'type': 'account'}}