Discussion:
Access request.POST of JSON API requests in AdminEmailHandler
Martin Tóth
2018-04-25 16:04:10 UTC
Permalink
I'm using logger handler that posts to Slack (based on this
<https://serafin.io/slack-django-errors/>). It works fine for regular
Django requests, but for any ApiView I can't access POST data. Here's what
happening:

- make a POST request to /api/whatever with some JSON data
- class based view that's handling /api/whatever raises an exception
- it propagates to my custom AdminEmailHandler via emit(self, record,
*args, **kwargs)
- record.request object's POST is empty

Here's how I understand it happens:
Because views.ApiView is parsing the original request's stream, that makes
it inaccessible afterwards. The original WSGIRequest's body is being parsed
using parsers.JSONParser into request.data that can be used in ApiView for
further processing. When an exception is raised, logger handler is given
the original request, accesible via record.request. But since that's been
read from, it can't be read again:
(Pdb) p record.request.POST
<QueryDict: {}>
(Pdb) p record.request.body
*** RawPostDataException: RawPostDataException(u"You cannot access body
after reading from request's data stream",)
(Pdb) p record.request.readlines()
[]
(Pdb) p record.request.content_type
'application/json'

I assume request.POST is empty, because the request is JSON. Attempting to
parse the contents again fails due to request.body constraint:
parser = JSONParser()
parsed = parser.parse(request.body)
fails with same RawPostDataException exception.

My question is, *how do I access what's been POSTed*? How do I parse body
of JSON request when processing it AdminEmailHandler.emit?

What I'm trying to achieve is have the actual POST data displayed in Slack
channel, when an exception happens. Even plain "mail_admins" handler
doesn't display any POST information (retrieved using Django's
ExceptionReporter).

I'm using Django 1.11.9 with DRF 3.7.1.

Thank you for your time,
Martin
--
You received this message because you are subscribed to the Google Groups "Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-rest-framework+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Jurgis Pralgauskis
2018-08-17 21:04:25 UTC
Permalink
I've run into very similar issue?

did you manage to solve it?

actually, if I would have drf request object, I could take request.data,

but seems, I have only django request object :/

there is proposal, how to get DRF request.data shown on debug error page,
https://stackoverflow.com/questions/24916207/show-request-data-in-django-500-error-caused-by-rest-framework/39024682#39024682
but this does not work with logging handler for me :/
--
You received this message because you are subscribed to the Google Groups "Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-rest-framework+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Loading...