0

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 -

6
  • Looks like your indentation on the last 7 lines is wrong, but that could be copy&paste error. What does the terminal show in which you do flask run?
    – Blauelf
    Jun 11, 2018 at 12:57
  • @Blauelf thanks for pointing that out. Fixed now. Jun 12, 2018 at 10:58
  • Error messages from terminal might be helpful, not just "500".
    – Blauelf
    Jun 12, 2018 at 12:05
  • @Blauelf added error text Jun 12, 2018 at 16:22
  • Maybe you could add 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.
    – Blauelf
    Jun 13, 2018 at 11:50

1 Answer 1

4

So it seems the workaround solution was to add

app.config['JSONIFY_PRETTYPRINT_REGULAR'] = False

somewhere after the

app = Flask(__name__)

The issue has been reported (https://github.com/pallets/flask/issues/2549, and duplicate https://github.com/pallets/flask/issues/2599), and is fixed in current release, so let's hope we get an updated version soon (as of v135, we're still on Flask 0.12.2 from May 2017, the bugfix happened in January 2018).

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .