Skip to content

Django integration: You cannot access body after reading from request's data stream #3045

Closed
@Audiopolis

Description

How do you use Sentry?

Sentry Saas (sentry.io)

Version

2.0.1

Steps to Reproduce

Environment info:
django=5.0.4
djangorestframework=3.15.1

  1. Add sentry integration to django app:
if ENV != "development":
    sentry_sdk.init(
        dsn="https://....ingest.sentry.io/6010199",
        integrations=[
            DjangoIntegration(
                transaction_style="url",
                middleware_spans=True,
                signals_spans=True,
                cache_spans=False,
            )
        ],
        debug=DEBUG,  # This is true in this case
        send_default_pii=True,
        environment=ENV,
    )
  1. `logger.exception("This is an exception")
  2. See traceback

Expected Result

Either the exception is successfully logged to Sentry, or it's not sent to Sentry at all since it's a log entry and not an unhandled exception.

Actual Result

# Traceback for the `logger.exception` call goes here

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/lib/python3.12/site-packages/sentry_sdk/integrations/django/__init__.py", line 555, in parsed_body
    return self.request.data
           ^^^^^^^^^^^^^^^^^
AttributeError: 'ASGIRequest' object has no attribute 'data'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/lib/python3.12/site-packages/sentry_sdk/integrations/django/asgi.py", line 56, in asgi_request_event_processor
    DjangoRequestExtractor(request).extract_into_event(event)
  File "/usr/local/lib/python3.12/site-packages/sentry_sdk/integrations/_wsgi_common.py", line 96, in extract_into_event
    parsed_body = self.parsed_body()
                  ^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/sentry_sdk/integrations/django/__init__.py", line 557, in parsed_body
    return RequestExtractor.parsed_body(self)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/sentry_sdk/integrations/_wsgi_common.py", line 142, in parsed_body
    return self.json()
           ^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/sentry_sdk/integrations/_wsgi_common.py", line 154, in json
    raw_data = self.raw_data()
               ^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/sentry_sdk/integrations/django/__init__.py", line 538, in raw_data
    return self.request.body
           ^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/django/http/request.py", line 328, in body
    raise RawPostDataException(
django.http.request.RawPostDataException: You cannot access body after reading from request's data stream

Activity

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

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions