Skip to content

Make sure everything works with Django-Rest-Framework #24

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
untitaker opened this issue Aug 16, 2018 · 7 comments
Closed

Make sure everything works with Django-Rest-Framework #24

untitaker opened this issue Aug 16, 2018 · 7 comments

Comments

@untitaker
Copy link
Member

untitaker commented Aug 16, 2018

We should django-rest-framework's request.data instead of trying to extract a structured body ourselves

@Skorpyon
Copy link

Skorpyon commented Oct 10, 2018

RawPostDataException
You cannot access body after reading from request's data stream

Way how you read request body kill Django-Stripe integration
https://github.com/dj-stripe/dj-stripe/blob/3eafe287ac93d94f2615ff8a5b53ddf7e5e9e46c/djstripe/models/webhooks.py#L81-85

@untitaker
Copy link
Member Author

I'm sorry, I can't reproduce this issue. Could you open a new issue, with a traceback?

@cwmoo740
Copy link

cwmoo740 commented Jan 9, 2019

It's somewhat difficult to understand why using sentry with django_rest_framework does not upload request bodies out of the box. There were a few scattered SO and github threads about the problem but the few that I found refer to the older sentry client. I managed to track down the problem and wanted to share my results.

  1. DRF uses its own json parser and calls request.read directly, without storing the result on django's request._body
  2. Sentry attempts to read from django's request.body, which then errors because the stream has already been read and not assigned to request._body

Here are relevant parts of the code that I found:

https://github.com/encode/django-rest-framework/blob/3.9.0/rest_framework/parsers.py#L68

https://github.com/django/django/blob/2.1.5/django/http/request.py#L283

One workaround is to do something silly like this:

@api_view(['POST'])
def my_view(request):
    request.body # side effect of storing the whole request body in memory on request._request._body
    do_something(request.data)

I put up a repo demonstrating this.

https://github.com/cwmoo740/django_sentry_debugging

It would be significantly better imo if there was a separate integration for DjangoRestFramework that simply read from request.data directly and let DRF do the parsing, as you suggested when you opened this thread.

If that work will be accepted I can tackle it and put up a pr.

@electroprovodka
Copy link

Hi, I've also faced this issue when using DRF 3.9.0 and sentry-sdk 0.7.8 (0.7.4 also has this). With new unified sdk request bodies aren't sent to Sentry UI.

I was able to track it down to the place where issue occurs.

When extracting data from request, sdk tries to get body here, but it's impossible due to the You cannot access body after reading from request's data stream error. It is raised when sdk is trying to read self.request.body inside the called function in django integration.

@untitaker
Copy link
Member Author

untitaker commented Apr 5, 2019

@cwmoo740 I'm very sorry I dropped the ball on this. Your investigation is very complete and I will fix this in the next release.

I think the primary confusion for me was that request.body does not get populated when request.data is accessed. I assumed that implementing access to request.data was not essential for being able to capture the request body of DRF apps.

@untitaker
Copy link
Member Author

0.7.10 was released which should fix this

@Audiopolis
Copy link

This is happening on 2.0.1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants