Open
Description
Checklist
- Raised initially as discussion "No POST data" in error report #7899
- This cannot be dealt with as a third party library. (We prefer new functionality to be in the form of third party libraries where possible.)
- I have reduced the issue to the simplest possible case.
Description
I'm using Django 3.1.7 & DRF 3.12.2.
When an exception occurs in a ViewSet, Django automatically sends an email to the administrator.
The problem is that the POST data is always empty in this email:
POST: No POST data
Instead, I expect to see the JSON payload that was sent in the request body.
I added a breakpoint in django.utils.log.AdminEmailHandler.emit(), and I can confirm that request.POST is empty.
But on the other hand, I'm sure that request.data was not empty in the ViewSet.
I verified that the request's Content-Type and X-Requested-With are correctly set.
The only non-standard middlewares I use are corsheaders and admin_reorder; I don't think they are related to this problem.
Reacted by Devid
Metadata
Metadata
Assignees
Labels
No labels
Activity
danielmoessner commentedon Jan 31, 2022
Any progress on this issue? I have the same problem.
Hafnernuss commentedon Mar 1, 2022
I can also confirm this issue, and it makes it a little bit hard to trace back errors
Here is a discussion related to this problem
rvinzent commentedon Apr 8, 2022
At this point I am convinced DRF is a dead project
stale commentedon Jun 12, 2022
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
bblanchon commentedon Jun 12, 2022
I still think this is an important feature, please don't close this issue.
Hafnernuss commentedon Jun 12, 2022
I agree.
markdawson commentedon Jul 29, 2022
I'm also having this issue.
stale commentedon Oct 1, 2022
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
bblanchon commentedon Oct 1, 2022
I still think this is an important feature, please don't close this issue.
donghj2000 commentedon Nov 14, 2022
It is because django dose not parse body of application/json to request.POST field.You can convert json string to dict by yourself, like this:
res = request.body
json_str = res.decode('utf-8')
json_dict = json.loads(json_str)
Then write a custom log handler class instead of AdminEmailHandler to send your own dict data converted from json.
donghj2000 commentedon Nov 14, 2022
some code snippet:
and the message is ...\n\nGET: No GET data\n\nPOST:\nusername = 'python1'\npassword = '123456'\n\nFILES: No FILES data...
bblanchon commentedon Nov 16, 2022
Thanks, @donghj2000, this is awesome, but I still think this should be a built-in feature.
donghj2000 commentedon Nov 17, 2022
Glad to help you. I have not found why they didn't do so. Another workaround is to use Middleware:
rvinzent commentedon Nov 17, 2022
This snippet has an issue where it assumes the JSON is an object and will fail on any other valid JSON value
donghj2000 commentedon Nov 17, 2022
It seems to be a bug exactly, but request.POST is initialized as a QueryDict(django.http.request.py->class HttpRequest) ,so I don't know how to deal with other JSON type...
auvipy commentedon Nov 30, 2022
no. it has 4 new more maintainers now. btw would love to see a failing test case to find out the fix for the problem. who is up for that? here is a comment pointing to the direction #1671 (comment)
jonnythebard commentedon Apr 28, 2024
@auvipy
I can do this but I'd like to know the definition of the problem from your side.
@donghj2000 said,
Do you agree that it is a problem and
request.POSTalso has to parse application/json?I am asking this because I thought there must be a DRF design reason behind
request.POSTis not parsing application/json.If you conceive this to be a bug rather than a DRF design, I can raise a fix PR.
cpastordev commentedon Jun 13, 2024
One workaround is to get DRF's APIView class to set the request POST field after parsing the request. This can be achieved by overriding initialize_request on the APIView (or ViewSet) class:
Note however that Django's decorator sensitive_post_parameters will not work with DRF's views as it is expecting Django's Request object, so you might want to set
request.sensitive_post_parametersto a list of sensitive parameter names at the same time.sevdog commentedon Sep 19, 2025
I was looking at this problem and caught #5800 which was the point in which the
ifwhich prevent the setting of files/data to be propagated to the original request.That PR refers to a bug introduced in #5590 (which was without that condition), however there are no clue about why this should be a bug (since here we are complaining that the absence of it is a bug):
zainnadeem786 commentedon Jun 15, 2026
Hi @auvipy,
I'd be interested in investigating this further.
Before proposing a fix, I'd like to verify the current behavior around #5590 and #5800 and determine whether the expected outcome is:
I'll try to reduce this to a failing test case first and report back with findings.
Thanks.
zainnadeem786 commentedon Jun 15, 2026
Hi @auvipy,
I investigated the current behavior on main and was able to reproduce the report.
From what I found, DRF correctly parses JSON into
request.data, while Django's originalHttpRequest.POSTremains empty. This appears to be intentional rather than a parsing bug.Looking through the history, #5590 preserved the empty POST behavior for JSON requests and #5800 further restricted synchronization to form media types. There are also existing tests protecting the current behavior.
Because of that, I'm not sure a failing test asserting JSON data appears in
request.POSTwould be aligned with the current design.Before preparing a PR, could you clarify whether the preferred direction would be:
request.POST, orI'd be happy to continue from there once the expected behavior is confirmed.
auvipy commentedon Sep 12, 2026
<i am open to a proper fix to this as this seems to be a regression, apparently
Expose parsed request data to ExceptionReporter on unhandled view err…