我安装了烧瓶并写了这个基本代码
from __future__ import print_function
from flask import Flask, jsonify, request
import json
import sys
app = Flask(__name__)
@app.route('/api/dashboard/create', methods=['POST'])
def build_dashboard():
if "application/json" not in request.headers["Content-Type"]:
abort(400)
print('test1', file=sys.stderr)
json_data = request.get_json()
print("test2", file=sys.stderr)
dashboard_data = {}
return dashboard_data, 201
然后,我使用curl发送像这样的json请求
curl -H "Content-Type: application/json" -X POST -d '{"username":"xyz","password":"xyz"}' http://localhost:5000/api/dashboard/create
我看到的输出是
test1
127.0.0.1 - - [23/Jul/2017 23:39:29] "POST /api/dashboard/create HTTP/1.1" 400
因此,对request.get_json()的调用会导致错误等。
我也尝试过调用request.json但是得到了相同的结果(我已经看到了一些 关于类似问题的问题,但总是request.json或 request.get_json()似乎对人有用。
当我打印request.is_json时,我得到了真的
我使用了烧瓶0.12.2 for windows,这应该是我认为的最新版本。
知道发生了什么事吗? 非常感谢:)
编辑:
在调用request.get_json()
之前调试Pdb步骤> c:\users\john\desktop\lively-backend\main.py(16)build_dashboard()
-> json_data = request.get_json()
(Pdb) n
BadRequest: <BadRequ...Request'>
> c:\users\john\desktop\lively-backend\main.py(16)build_dashboard()
-> json_data = request.get_json()
(Pdb) n
--Return--
> c:\users\john\desktop\lively-backend\main.py(16)build_dashboard()->None
-> json_data = request.get_json()
(Pdb) n
BadRequest: <BadRequ...Request'>
> c:\python27\lib\site-packages\flask-0.12.2-py2.7.egg\flask\app.py(1598)dispatch_request()
-> return self.view_functions[rule.endpoint](**req.view_args)
(Pdb) n
--Return--
> c:\python27\lib\site-packages\flask-0.12.2-py2.7.egg\flask\app.py(1598)dispatch_request()->None
-> return self.view_functions[rule.endpoint](**req.view_args)
(Pdb) n
BadRequest: <BadRequ...Request'>
> c:\python27\lib\site-packages\flask-0.12.2-py2.7.egg\flask\app.py(1612)full_dispatch_request()
-> rv = self.dispatch_request()
(Pdb) n
> c:\python27\lib\site-packages\flask-0.12.2-py2.7.egg\flask\app.py(1613)full_dispatch_request()
-> except Exception as e:
(Pdb) n
> c:\python27\lib\site-packages\flask-0.12.2-py2.7.egg\flask\app.py(1614)full_dispatch_request()
-> rv = self.handle_user_exception(e)
(Pdb) n
> c:\python27\lib\site-packages\flask-0.12.2-py2.7.egg\flask\app.py(1615)full_dispatch_request()
-> return self.finalize_request(rv)
(Pdb) n
--Return--
> c:\python27\lib\site-packages\flask-0.12.2-py2.7.egg\flask\app.py(1615)full_dispatch_request()-><Respons...REQUEST]>
-> return self.finalize_request(rv)
(Pdb) n
> c:\python27\lib\site-packages\flask-0.12.2-py2.7.egg\flask\app.py(1989)wsgi_app()
-> return response(environ, start_response)
(Pdb) n
> c:\python27\lib\site-packages\flask-0.12.2-py2.7.egg\flask\app.py(1991)wsgi_app()
-> if self.should_ignore_error(error):
(Pdb) n
> c:\python27\lib\site-packages\flask-0.12.2-py2.7.egg\flask\app.py(1993)wsgi_app()
-> ctx.auto_pop(error)
(Pdb) n
--Return--
> c:\python27\lib\site-packages\flask-0.12.2-py2.7.egg\flask\app.py(1993)wsgi_app()-><werkzeu...04467B38>
-> ctx.auto_pop(error)
(Pdb) n
--Return--
> c:\python27\lib\site-packages\flask-0.12.2-py2.7.egg\flask\app.py(1997)__call__()-><werkzeu...04467B38>
-> return self.wsgi_app(environ, start_response)
(Pdb) n
> c:\python27\lib\site-packages\werkzeug\serving.py(198)execute()
-> try:
(Pdb) n
> c:\python27\lib\site-packages\werkzeug\serving.py(199)execute()
-> for data in application_iter:
(Pdb) n
> c:\python27\lib\site-packages\werkzeug\serving.py(200)execute()
-> write(data)