flask request debugging flask request debugging flask flask

flask request debugging


You can use the pprint module.

Printing the request object itself won't show any useful information, however, so you will probably want to print the request.environ instead.

As an example:

from flask import Flask, Response, requestimport pprintapp = Flask(__name__)@app.route("/test")def test():    str = pprint.pformat(request.environ, depth=5)    return Response(str, mimetype="text/text")app.run(debug=True)

Yields:

{'CONTENT_LENGTH': '', 'CONTENT_TYPE': '', 'HTTP_ACCEPT': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8', 'HTTP_ACCEPT_ENCODING': 'gzip,deflate,sdch', 'HTTP_ACCEPT_LANGUAGE': 'en-US,en;q=0.8,es;q=0.6', 'HTTP_CACHE_CONTROL': 'max-age=0', 'HTTP_CONNECTION': 'keep-alive', 'HTTP_COOKIE': 'session=eyJsYXN0X2FwcGlkIjoiMiI', 'HTTP_HOST': '127.0.0.1:5000', 'HTTP_USER_AGENT': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.94 Safari/537.36', 'PATH_INFO': '/test', 'QUERY_STRING': '', 'REMOTE_ADDR': '127.0.0.1', 'REMOTE_PORT': 36547, 'REQUEST_METHOD': 'GET', 'SCRIPT_NAME': '', 'SERVER_NAME': '127.0.0.1', 'SERVER_PORT': '5000', 'SERVER_PROTOCOL': 'HTTP/1.1', 'SERVER_SOFTWARE': 'Werkzeug/0.9.4', 'werkzeug.request': <Request 'http://127.0.0.1:5000/test' [GET]>, 'werkzeug.server.shutdown': <function shutdown_server at 0x10cd1b8>, 'wsgi.errors': <open file '<stderr>', mode 'w' at 0x7f5211b911e0>, 'wsgi.input': <socket._fileobject object at 0x1083f50>, 'wsgi.multiprocess': False, 'wsgi.multithread': False, 'wsgi.run_once': False, 'wsgi.url_scheme': 'http', 'wsgi.version': (1, 0)}


Well you can print request object as a dict and see the info there, but I would suggest to try Flask Debug Toolbar, it could be helpful to see all the request data and more. This toolbar is the port of the Django debug toolbar.