1

I need equivalent of Flask's request.get_data() for Django / DRF. Got it almost working with str.encode(json.dumps(request.data)), but the output contains spaces between pairs key: value and existing link's signs are not escaped.

Flask output (that works for me):

b'{"token":"xxxx","team_id":"TDB6U9XM1","api_app_id":"yyyyyy","event":{"type":"link_shared","user":"UDC5TRW0M","channel":"CE9GC5C4R","message_ts":"1543451681.002600","links":[{"url":"http:\\/\\/testing.serv-dev.pl\\/viewer\\/3\\/24","domain":"serv-dev.pl"}]},"type":"event_callback","event_id":"EvEE40NTSM","event_time":1543451682,"authed_users":["UDC5TRW0M"]}'

Current Django output:

b{"token": "xxxx", "team_id": "TDB6U9XM1", "api_app_id": "yyyyyy", "event": {"type": "link_shared", "user": "UDC5TRW0M", "channel": "CE9GC5C4R", "message_ts": "1543452606.003600", "links": [{"url": "http://testing.serv-dev.pl/viewer/3/24", "domain": "serv-dev.pl"}]}, "type": "event_callback", "event_id": "EvEENV65TU", "event_time": 1543452607, "authed_users": ["UDC5TRW0M"]}'.

This is specifically for slack request signature verification that specifically requires raw request body to be passed.

Accessing request.body throws an error django.http.request.RawPostDataException: You cannot access body after reading from request's data stream probably due to POST method.

davidism
  • 121,510
  • 29
  • 395
  • 339
  • `request.body` *is* the right way. Just don't read `request.POST` first. – Daniel Roseman Nov 29 '18 at 09:46
  • Yes, this is a case. `request.body` cannot be accessed after using not only `request.POST`, but also `request.data` (this was my case). Problem solved. –  Nov 29 '18 at 10:20

0 Answers0