Reputation: 1083
I've been looking at this for a while now, I'm sending out a post request with multipart/form-data, but get back the error ["This field is required."] for each field that I'm supposed to populate with the request.
Here's the Serializer:
class InvoiceUploadSerializer(serializers.Serializer):
serial=serializers.CharField(max_length=256, required=True)
amount=serializers.CharField(max_length=256, required=True)
debtor=serializers.CharField(max_length=256, required=True)
dateout=serializers.CharField(max_length=256, required=True)
expiration=serializers.CharField(max_length=256, required=True)
invoicefile=serializers.FileField()
class Meta:
fields=('serial', 'amount', 'debtor', 'dateout', 'expiration', 'invoicefile',)
And the View:
class InvoiceViewSet(viewsets.ModelViewSet):
queryset=Invoices.objects.all()
serializer_class=InvoiceSerializer
parser_classes=(MultiPartParser, FormParser)
def get_permissions(self):
if self.request.method in permissions.SAFE_METHODS:
return (permissions.AllowAny(),)
if self.request.method == 'POST':
return (permissions.IsAuthenticated(),)
return (permissions.IsAuthenticated(), IsAccountOwner(),)
def create(self, request):
serializer=InvoiceUploadSerializer(data=request.data)
if serializer.is_valid():
... handle serializer
return Response(serializer.validated_data, status=status.HTTP_201_CREATED)
else:
emessage=serializer.errors
return Response({
'status': 'Bad request',
'message': emessage,
}, status=status.HTTP_400_BAD_REQUEST
Here is a picture showing the browser console with the outgoing request:
And lastly the error message from the browser console:
I don't understand why it tells me all fields are missing, the header is set correctly and it looks fine to me.
Switched up the view-code and urls still the same error, New with the same problem:
class InvoiceUploadView(APIView):
parser_classes=(MultiPartParser, FormParser)
def get_permissions(self):
return (permissions.AllowAny(),)
def post(self, request):
serializer=InvoiceUploadSerializer(data=request.POST)
if serializer.is_valid():
return Response(serializer.validated_data, status=status.HTTP_201_CREATED)
else:
emessage=serializer.errors
return Response({
'status': 'Bad request',
'message': emessage,
}, status=status.HTTP_400_BAD_REQUEST)
Upvotes: 1
Views: 5823
Reputation: 23004
Your POST request is missing the multipart boundary in it's Content-Type
header. Without that it is possible that the application won't be able to parse the request payload - and validation would fail because data would be missing.
The browser would normally set the Content-Type
header and boundary for you. Perhaps you are overriding that and setting the Content-Type
header yourself somewhere? If you are, unset it and try making another request.
Upvotes: 1