0

whenever I post a valid request to create view I get { "detail": "JSON parse error - Expecting value: line 1 column 1 (char 0)" } instead of the custom responses I setup

class ServerView(generics.CreateAPIView):
    queryset = Server.objects.all()
    serializer_class = ServerSerializer

class CreateView(APIView):
    serializers_class = CreateSerializers

    def post(self, request, format=None):
        serializer = self.serializers_class(data=request.data)
        if serializer.is_valid():
            name = serializer.data.get(name)
            queryset = Server.objects.filter(name=name)
            if not queryset.exists():
                server = Server(name=name)
                server.save()
                return Response(ServerSerializer(server).data, status=status.HTTP_201_CREATED)
            return Response({'Bad Request': 'Join or try another name'}, status=status.HTTP_226_IM_USED)
        return Response({'Bad Request': 'Name is not valid'}, status=status.HTTP_400_BAD_REQUEST)```
Anonymous
  • 21
  • 2
  • 1
    Could you provide a [MRE](https://stackoverflow.com/help/minimal-reproducible-example)? I cannot seem to recreate this problem, everything works fine. What does **whenever I post a valid request** mean? How are you sending the request? – Countour-Integral Jan 18 '21 at 19:35
  • @Countour-Integral I created a view out of the createview class and then manually post an random server name. if the doesn't server exists it should return status 201 or 226 if not, but I'm always getting the weird error – Anonymous Jan 18 '21 at 19:42
  • Could you provide the exact request that you are sending? As it seems your client is sending invalid `JSON` or it can not be parsed by django. This seems to be the case for [here](https://stackoverflow.com/questions/55439411/detail-json-parse-error-expecting-value-line-1-column-1-char-0). – Countour-Integral Jan 18 '21 at 19:47

0 Answers0