flask request debugging


Question

Is there a way to print a request in flask as a string for debugging purposes? I'd simply like to print all params of a post request without having to write my own formatter for the request object.

1
6
2/5/2019 4:54:36 PM

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, request
import pprint

app = 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)}
8
10/1/2014 10:44:41 AM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon