Reputation: 110237
I am able to parse the following using django's request.POST
:
>>> r = requests.POST(url, data={'name': 'david'})
# in view
name = request.POST.get('name') # 'david'
However, if I encode that data as json, I'm not able to get it and I have to use request.body
:
>>> r = requests.POST(url, data=json.dumps({'name': 'david'}))
# in view
name = request.POST.get('name') # empty string
name = request.body.get('name') # 'david'
Why doesn't django parse the POST data here if it's in json?
Upvotes: 0
Views: 51
Reputation: 6796
request.POST
is meant to access conventional html form data, and request.body
for all other formats (xml, json, etc.). raw_post_data
is deprecated in newer django versions, it's successor is request.body
. Also, you would have to deserialize the incoming json data before accessing it even through request.body
, e.g.: json.loads(request.body)
More information can be found about both HttpRequest.body
and HttpRequest.POST
in the official documentation:
https://docs.djangoproject.com/en/dev/ref/request-response/#django.http.HttpRequest.body
Upvotes: 1