master
branch of Django REST framework.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.
result of both pprint : {u'code': u'print 12dgdf3'}
<QueryDict: {}>
result of both pprint : {u'code': u'print 12dgdf3'}
{u'code': u'print 12dgdf3'}
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?
Most helpful comment
Correct, yup. Use
request.data
.