I have a middleware where I a want to log every request/response. How can I access to POST data?
django, rest
Solution
Here is complete solution I made
"""
Api middleware module
"""
import logging
request_logger = logging.getLogger('api.request.logger')
class LoggingMiddleware(object):
"""
Provides full logging of requests and responses
"""
_initial_http_body = None
def process_request(self, request):
self._initial_http_body = request.body # this requires because for some reasons there is no way to access request.body in the 'process_response' method.
def process_response(self, request, response):
"""
Adding request and response logging
"""
if request.path.startswith('/api/') and \
(request.method == "POST" and
request.META.get('CONTENT_TYPE') == 'application/json'
or request.method == "GET"):
request_logger.log(logging.DEBUG,
"GET: {}. body: {} response code: {}. "
"response "
"content: {}"
.format(request.GET, self._initial_http_body,
response.status_code,
response.content), extra={
'tags': {
'url': request.build_absolute_uri()
}
})
return response
Note, this
'tags': {
'url': request.build_absolute_uri()
}
will allow you to filter by url in sentry.
Problem
I have this middleware ``` import logging request_logger = logging.getLogger('api.request.logger') class LoggingMiddleware(object): def process_response(self, request, response): request_logger.log(logging.DEBUG, "GET: {}. POST: {} response code: {}. response " "content: {}".format(request.GET, request.DATA, response.status_code, response.content)) return response ``` The problem is that request in process_response method has no .POST nor .DATA nor .body. I am using django-rest-framework and my requests has Content-Type: application/json Note, that if I put logging to process_request method - it has .body and everything I need. However, I need both request and response in a single log entry.