Django-rest-framework: request.POST is empty querydict when data posted using browsable api, when posted with hhtpie .. its fine

Created on 11 Apr 2016  ·  4Comments  ·  Source: encode/django-rest-framework

Checklist

  • [x ] I have verified that that issue exists against the master branch of Django REST framework.
  • [x ] I have searched for similar issues in both open and closed tickets and cannot find a duplicate.
  • [x] This is not a usage question. (Those should be directed to the discussion group instead.)
  • [x] This cannot be dealt with as a third party library. (We prefer new functionality to be in the form of third party libraries where possible.)
  • [x] I have reduced the issue to the simplest possible case.
  • [x] I have included a failing test as a pull request. (If you are unable to do so we can still accept the issue.)

    Steps to reproduce

step 1 . When data posted to apiview , pprint the POST querydict.

@api_view(['GET', 'POST'])
def snippet_list(request):
if request.method == 'POST':
from pprint import pprint as pp
pp(request.data)
pp(request.POST)
serializer = SnippetSerializer(data=request.data)
if serializer.is_valid():
serializer.save()
return Response(serializer.data, status=status.HTTP_201_CREATED)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

step 2 . Post '{"code": "print 123"}' from browsable api to snippet_list apiview.

Expected behavior

result of both pprint : {u'code': u'print 12dgdf3'}
<QueryDict: {}>

Actual behavior

result of both pprint : {u'code': u'print 12dgdf3'}
{u'code': u'print 12dgdf3'}

Most helpful comment

mybad .. only form-data is is available in in request.POST

Correct, yup. Use request.data.

All 4 comments

mybad .. only form-data is is available in in request.POST , I assumed any data sent using Http POST will be in request.POST . closing this issue, tnx

mybad .. only form-data is is available in in request.POST

Correct, yup. Use request.data.

if someone is interested where to find the official answer:

https://www.django-rest-framework.org/tutorial/2-requests-and-responses/#request-objects

I still don't understand why POST is empty. :(
Any tutorial or link?

Was this page helpful?
0 / 5 - 0 ratings