2

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:

Localhost json

On Heroku:

Heroku json

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

President James K. Polk
  • 40,516
  • 21
  • 95
  • 125
ItzSylex
  • 23
  • 3
  • server may runs it in different way as you expect - it may inport `app` and run it with own parameters - instead of using your `app.run(debug=True)` - If you want to pretty output then better convert to text or HTML and send this text, HTML. OR you may have to set `debug` in different way - probably `app.config["DEBUG"] = True` – furas Sep 07 '20 at 03:21
  • you can also save `jsonify(data) ` in file and later see if it creates format. You can also check config before `jsonify(data)` to see if it have correct settings. You can even try to set new setting directly before `jsonify(data)`. BTW: you can save in files both results in web browser and check if both have the same formatting. – furas Sep 07 '20 at 03:28
  • BTW: you could add minimal working code - so we could run it - and URL to working code on heroku - so we could see what it sends. – furas Sep 07 '20 at 03:30
  • Thanks for replying @furas, I have edited the post above with some simple test code I ran both on my localhost and on heroku, here is the output on heroku https://jojoapi.herokuapp.com/test. Heroku logs are also printing Content-Type: application/json Also, not sure if it adds up, here is the repo to the project https://github.com/itzsylex/jojoAPI – ItzSylex Sep 07 '20 at 04:07
  • I have just set `debug` like you suggested `app.config["DEBUG"] = True` and it is working as it should. Thanks! – ItzSylex Sep 07 '20 at 04:11

1 Answers1

1

Some servers (not only Heroku) may not run directly your script and not execute app(debug=True) but they may import app to own code and run it with own arguments app(...own args...) - and this can makes problem.

You can set debug mode in code in different method.

app.config["DEBUG"] = True

Eventually you can try to set environment variable in Linux

export FLASK_DEBUG=1

or

export FLASK_ENV=development

See doc: Debug Mode

Flask doc: Standalone WSGI Containers - it shows servers which import app (as myproject:app) and they may runs with own arguments.

furas
  • 134,197
  • 12
  • 106
  • 148