I installed flask and wrote this basic code
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
Then, I used curl to send a json request like this
curl -H "Content-Type: application/json" -X POST -d '{"username":"xyz","password":"xyz"}' http://localhost:5000/api/dashboard/create
The output I see is
test1
127.0.0.1 - - [23/Jul/2017 23:39:29] "POST /api/dashboard/create HTTP/1.1" 400
So the call to request.get_json() causes an error or something.
I tried also calling request.json but I got the same result (I've saw some questions here regarding a similar issue, but always request.json or request.get_json() seemed to work for people.
When I printed request.is_json I got True
I'm using flask 0.12.2 for windows, which should be the newest version I think.
Any idea what's happening? Thanks a lot :)
edit:
Pdb step debugging before the call to request.get_json()
> 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)