5

文档说要使用,request.get_json()但这会导致服务返回错误:

Server returned HTTP response code: 500

但是,呼叫request.datarequest.json两者都起作用。

堆栈跟踪:

Traceback (most recent call last):
  File "/home/blake/ves/p27/lib/python2.7/site-packages/flask/app.py", line 1701, in __call__
    return self.wsgi_app(environ, start_response)
  File "/home/blake/ves/p27/lib/python2.7/site-packages/werkzeug/contrib/fixers.py", line 125, in __call__
    return self.app(environ, start_response)
  File "/home/blake/ves/p27/lib/python2.7/site-packages/flask/app.py", line 1689, in wsgi_app
    response = self.make_response(self.handle_exception(e))
  File "/home/blake/ves/p27/lib/python2.7/site-packages/flask/app.py", line 1687, in wsgi_app
    response = self.full_dispatch_request()
  File "/home/blake/ves/p27/lib/python2.7/site-packages/flask/app.py", line 1360, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/home/blake/ves/p27/lib/python2.7/site-packages/flask/app.py", line 1358, in full_dispatch_request
    rv = self.dispatch_request()
  File "/home/blake/ves/p27/lib/python2.7/site-packages/flask/app.py", line 1344, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/home/blake/workspace/starcycle-flask/starcycleweb.py", line 17, in api
    print request.get_json()
  File "/home/blake/ves/p27/lib/python2.7/site-packages/werkzeug/local.py", line 336, in __getattr__
    return getattr(self._get_current_object(), name)
AttributeError: 'Request' object has no attribute 'get_json'

我认为 get_json 是获取 json 数据的首选方式。

4

2 回答 2

14

在 Flask/wrappers.py 中,方法get_json()被定义为类的方法Request

但是,同一个文件仍然包含旧的、已弃用的方法json()。如果您有旧版本的烧瓶,那么 a) 更新或 b)request.json()改用。

于 2013-07-05T15:13:24.617 回答
6

我对烧瓶响应对象有类似的错误,它最终成为版本问题,我们使用烧瓶 0.12.1 并且 get_json() 在 1.0.2 上可用

文件:

http://flask.pocoo.org/docs/0.12/api/#response-objects

VS

http://flask.pocoo.org/docs/1.0/api/#response-objects

于 2018-09-09T06:44:52.223 回答