I am trying to return a JSON on a certain URL in python application. Without this line db = SQL("sqlite:///mashup.db")
everything works. The JSON is returned correctly.
If I uncomment that line, however, I get an Internal Server Error pointing to jsonify() method.
Here's the whole example:
from flask import Flask, jsonify, json, render_template, request
from cs50 import SQL
app = Flask(__name__)
# THE LINE THAT BREAKS THE APPLICATION
#db = SQL("sqlite:///mashup.db")
@app.route("/g")
def getEmployeeList():
employeeList = []
# fill up the list with some data
for i in range(0,2):
empDict = {
'firstName': 'Roy',
'lastName': 'Augustine'
}
employeeList.append(empDict)
return jsonify(employeeList)
If you uncomment line 7: db = SQL("sqlite:///mashup.db")
than the program (url /g
) responds with 500. This is the error text from terminal:
Traceback (most recent call last): File "/opt/pyenv/versions/3.6.0/lib/python3.6/site-packages/flask/app.py", line 1982, in wsgi_app response = self.full_dispatch_request() File "/opt/pyenv/versions/3.6.0/lib/python3.6/site-packages/flask/app.py", line 1614, in full_dispatch_request rv = self.handle_user_exception(e) File "/opt/pyenv/versions/3.6.0/lib/python3.6/site-packages/flask/app.py", line 1517, in handle_user_exception reraise(exc_type, exc_value, tb) File "/opt/pyenv/versions/3.6.0/lib/python3.6/site-packages/flask/_compat.py", line 33, in reraise raise value File "/opt/pyenv/versions/3.6.0/lib/python3.6/site-packages/flask/app.py", line 1612, in full_dispatch_request rv = self.dispatch_request() File "/opt/pyenv/versions/3.6.0/lib/python3.6/site-packages/flask/app.py", line 1598, in dispatch_request return self.view_functionsrule.endpoint
File "/home/ubuntu/workspace/pset8/mashup/test/application.py", line 25, in getEmployeeList return jsonify(employeeList)
File "/opt/pyenv/versions/3.6.0/lib/python3.6/site-packages/flask/json.py", line 251, in jsonify if current_app.config['JSONIFY_PRETTYPRINT_REGULAR'] and not request.is_xhr: File "/opt/pyenv/versions/3.6.0/lib/python3.6/site-packages/werkzeug/local.py", line 347, in getattr return getattr(self._get_current_object(), name) File "/opt/pyenv/versions/3.6.0/lib/python3.6/site-packages/werkzeug/wrappers.py", line 699, in is_xhr ), stacklevel=2)
DeprecationWarning: Request.is_xhr is deprecated. Given that the X-Requested-With header is not a part of any spec, it is not
INFO:werkzeug:10.240.0.187 - - [12/Jun/2018 16:15:43] "GET /g HTTP/1.1" 500 -
flask run
?app.config['JSONIFY_PRETTYPRINT_REGULAR'] = False
, this would deactivate the part that's throwing the warning (JSON won't look that nice). No idea how this could be connected to the database. Also, it's a warning, not an error. Not sure if that makes any difference, though.