I am building a simple flask app, jsonify()
works fine on my localhost, it will return the information with new lines and the proper indent in a json format. However, when running the exact same code on Heroku, it omits the new lines and the indentation.
On localhost:
On Heroku:
This is mentioned in the docs for jsonify()
This function's response will be pretty printed if the JSONIFY_PRETTYPRINT_REGULAR config parameter is set to True or the Flask app is running in debug mode
I have both set
app.config['JSONIFY_PRETTYPRINT_REGULAR'] = True
app.run(debug=True)
I tried manually setting the content type to application/json, but that did not help, I even tried using json.dumps()
and got the same result
return jsonify(data), 200, {'Content-Type': 'application/json; charset=utf-8'}
Any input on what could be causing heroku not pretty printing?
Edit:
from flask import request, jsonify, Flask
app = Flask(__name__)
@app.route('/test', methods = ['GET'])
def test():
test_dict = {"Key1": "Value1", "Key2": ["Value2","Value2","Value2",]}
print(jsonify(test_dict).headers)
return jsonify(test_dict)
if __name__ == '__main__':
app.run(debug=True)
This simple flask app would pretty print on my localhost like the photos linked above, however on heroku it wont. Looks like it is returning plain text. It can be seen here https://jojoapi.herokuapp.com/test. I am using gunicorn, not sure if that has any impacts on the output
Edit 2
So, I set manually debug to True as suggested in the comments app.config["DEBUG"] = True
and it works properly on heroku now
Some servers (not only Heroku) may not run directly your script and not execute
app(debug=True)
but they may importapp
to own code and run it with own argumentsapp(...own args...)
- and this can makes problem.You can set debug mode in code in different method.
Eventually you can try to set environment variable in Linux
or
See doc: Debug Mode
Flask doc: Standalone WSGI Containers - it shows servers which import
app
(asmyproject:app
) and they may runs with own arguments.