Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django cannot parse POST parameters of WSGIRequest on Internal Server Errors

I'm using Django REST Framework and all the API calls come from Android and iOS apps. The system works perfectly most of the time, however, when an internal server error happens and I get an email from Django, the POST of the WSGIRequest contains <could not parse> instead of the actual posted JSON data (even though 'CONTENT_TYPE': 'application/json' is also in the header, and the data is sent as JSON).

This is really annoying, as it would be great to see the request body that actually causes the error, not just the stacktrace.

The <could not parse> part is very similar to this question (in the ModPythonRequest part): django request.POST contains <could not parse>, except the actual problem is slightly different. Also the reference link in that question (https://stackoverflow.com/questions/12471661/mod-python-could-not-parse-the-django-post-request-for-blackberry-and-some-andro) seems to have gone down, even though the name looked very promising.

I'm on Django 1.6.2 and DRF 2.3.13.

like image 514
gregoltsov Avatar asked Dec 12 '22 04:12

gregoltsov


1 Answers

The POST dictionary of the WSGIRequest is always going to be invalid, because it is intended to hold the parsed form data when the Content-Type is application/x-www-form-urlencoded or multipart/form-data.

The data you want is in the body attribute of the WSGIRequest object, which isn't output when that object is converted to a string to be written to the log.

like image 168
Jamie Cockburn Avatar answered Dec 22 '22 15:12

Jamie Cockburn