Skip to content

"No POST data" in error report #7953

Open

Description

@bblanchon

Checklist

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.

Activity

  1. danielmoessner commented on Jan 31, 2022

    @danielmoessner

    Any progress on this issue? I have the same problem.

  2. Hafnernuss commented on Mar 1, 2022

    @Hafnernuss

    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

  3. rvinzent commented on Apr 8, 2022

    @rvinzent

    At this point I am convinced DRF is a dead project

  4. stale commented on Jun 12, 2022

    @stale

    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.

  5. bblanchon commented on Jun 12, 2022

    @bblanchon
    Author

    I still think this is an important feature, please don't close this issue.

  6. Hafnernuss commented on Jun 12, 2022

    @Hafnernuss

    I agree.

  7. markdawson commented on Jul 29, 2022

    @markdawson

    I'm also having this issue.

  8. stale commented on Oct 1, 2022

    @stale

    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.

  9. bblanchon commented on Oct 1, 2022

    @bblanchon
    Author

    I still think this is an important feature, please don't close this issue.

  10. donghj2000 commented on Nov 14, 2022

    @donghj2000

    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.

  11. donghj2000 commented on Nov 14, 2022

    @donghj2000

    some code snippet:

     def emit(self, record):
            try:
                request = record.request
                subject = '%s (%s IP): %s' % (
                    record.levelname,
                    ('internal' if request.META.get('REMOTE_ADDR') in settings.INTERNAL_IPS
                     else 'EXTERNAL'),
                    record.getMessage()
                )
    
                import json
                from django.http import QueryDict
                json_str = request.body.decode('utf-8')
                json_dict = json.loads(json_str)
                query_dict = QueryDict(mutable=True)
                for key in json_dict.keys():
                    query_dict[key] = json_dict[key]
                request.POST = query_dict
                
            except Exception as ex:
                subject = '%s: %s' % (
                    record.levelname,
                    record.getMessage()
                )
                request = None
            subject = self.format_subject(subject)
    

    and the message is ...\n\nGET: No GET data\n\nPOST:\nusername = 'python1'\npassword = '123456'\n\nFILES: No FILES data...

  12. bblanchon commented on Nov 16, 2022

    @bblanchon
    Author

    Thanks, @donghj2000, this is awesome, but I still think this should be a built-in feature.

  13. donghj2000 commented on Nov 17, 2022

    @donghj2000

    Glad to help you. I have not found why they didn't do so. Another workaround is to use Middleware:

    class ParseJsonData(MiddlewareMixin):
        def process_request(self, request):
            if request.content_type.startswith("application/json"):
                try:
                    json_str = request.body.decode('utf-8')
                    json_dict = json.loads(json_str)
                    query_dict = QueryDict(mutable=True)
                    for key in json_dict.keys():
                        query_dict[key] = json_dict[key]
                    request.POST = query_dict
                except Exception as ex:
                    pass
    
  14. rvinzent commented on Nov 17, 2022

    @rvinzent

    Glad to help you. I have not found why they didn't do so. Another workaround is to use Middleware:

    class ParseJsonData(MiddlewareMixin):
        def process_request(self, request):
            if request.content_type.startswith("application/json"):
                try:
                    json_str = request.body.decode('utf-8')
                    json_dict = json.loads(json_str)
                    query_dict = QueryDict(mutable=True)
                    for key in json_dict.keys():
                        query_dict[key] = json_dict[key]
                    request.POST = query_dict
                except Exception as ex:
                    pass
    

    This snippet has an issue where it assumes the JSON is an object and will fail on any other valid JSON value

  15. donghj2000 commented on Nov 17, 2022

    @donghj2000

    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...

  16. auvipy commented on Nov 30, 2022

    @auvipy
    Collaborator

    At this point I am convinced DRF is a dead project

    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)

  17. jonnythebard commented on Apr 28, 2024

    @jonnythebard

    @auvipy

    would love to see a failing test case to find out the fix for the problem

    I can do this but I'd like to know the definition of the problem from your side.

    @donghj2000 said,

    It is because django dose not parse body of application/json to request.POST field.

    Do you agree that it is a problem and request.POST also has to parse application/json?
    I am asking this because I thought there must be a DRF design reason behind request.POST is not parsing application/json.
    If you conceive this to be a bug rather than a DRF design, I can raise a fix PR.

  18. cpastordev commented on Jun 13, 2024

    @cpastordev

    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:

        def initialize_request(self, request, *args, **kwargs):
            drf_request = super().initialize_request(request, *args, **kwargs)
            request.POST = drf_request.data
            return drf_request
    

    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_parameters to a list of sensitive parameter names at the same time.

  19. sevdog commented on Sep 19, 2025

    @sevdog
    Contributor

    I was looking at this problem and caught #5800 which was the point in which the if which 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):

    request POST/FILES were also set for requests that did not have a form media type

  20. zainnadeem786 commented on Jun 15, 2026

    @zainnadeem786
    Contributor

    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:

    1. Propagating parsed JSON data back to the underlying Django request.POST for error reporting purposes, or
    2. Preserving the current separation between request.POST and DRF request.data and addressing the admin email use case differently.

    I'll try to reduce this to a failing test case first and report back with findings.

    Thanks.

  21. zainnadeem786 commented on Jun 15, 2026

    @zainnadeem786
    Contributor

    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 original HttpRequest.POST remains 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.POST would be aligned with the current design.

    Before preparing a PR, could you clarify whether the preferred direction would be:

    1. A DRF-aware error-reporting/reporter integration that exposes parsed request data without modifying request.POST, or
    2. Documentation explaining the limitation and recommended customization points?

    I'd be happy to continue from there once the expected behavior is confirmed.

  22. auvipy commented on Sep 12, 2026

    @auvipy
    Collaborator

    <i am open to a proper fix to this as this seems to be a regression, apparently

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions