How can I register a POST body request in Flask?
I am using a flask server and I want to log every data request and header (so I can use it to test my server). I took the logger werkzeug with
self._app.log = logging.getLogger('werkzeug')
self._app.log.addHandler(RotatingFileHandler('log.txt', mode='w'))
self._app.log.setLevel(logging.DEBUG)
But I don't understand how to change the format of the log to include request.data and request.headers, all I have is the default log
127.0.0.1 - - [17/Feb/2015 17:09:43] "POST /helloworld HTTP/1.1" 200 -
+3
eplaut
source
to share
2 answers
You can record additional information for each request using a Flask.before_request
hook:
@app.before_request
def log_request_info():
app.logger.debug('Headers: %s', request.headers)
app.logger.debug('Body: %s', request.get_data())
+12
Martijn pieters
source
to share
How about creating a little helper method that you call in every controller in your flask app.
The helper method would be something like this:
def log_my_request_data(request_data):
#this method will log this data
and then in all controllers, get request.data like this
from flask import request
request_data = request.data
and call log_my_request_data(request_data)
+1
NIlesh Sharma
source
to share