[Fixed]-POST JSON request data dropped/disappear when sending to djangorestframework

1👍

I don’t know about NOSQL but you can test it on shell. I’m guessing probably it works.
About request.data error; request.data is only used for file upload with django rest framework. When you post a json, it’s in the body of the request. What you should do is something like this in your view:

from django.views.decorators.csrf import csrf_exempt
from django.http import HttpResponse
import json

@csrf_exempt
class TestView(APIView):

# your get request here.

    def post(self, request, format=None):
        body_unicode = request.body.decode('utf-8')
        data = json.loads(body_unicode)
        # Now, your json content is stores in data as a dictionary.
        serializer = TestSerializer(data=request.data)
        if serializer.is_valid():
            # 4. can save() be overrided and do custom implementation? How?
            serializer.save()
            return Response(serializer.data, status=status.HTTP_201_CREATED)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

FYI,I use serializers for just creating json, I don’t use it to create object. Therefore, I’m not sure if this version %100 works or not. But I’m pretty sure that 2 line I’ve added turns JSON to dictionary. If serializer accepts dictionary as a data, it’ll work.

Leave a comment