1

I'm trying to create endpoint for uploading images, in my api, which i'm building with django rest framework. When I try to test the endpoint with postman, i'm getting response

"image": [
        "The submitted data was not a file. Check the encoding type on the form."
    ]

with status code 400. When I try to print variable with image to console I get

[<TemporaryUploadedFile: test.jpg (image/jpeg)>]

I've checked out some tutorials and I think i'm sending the file correctly. That is my post man configuration

that's the view

class ImageView(APIView):
    parser_classes = (MultiPartParser, )
    permission_classes = (IsAuthenticated, IsRestaurant)

    def post(self, request, *args, **kwargs):

        data = {
            'image': request.data.pop('image'),
            'info': request.user.info.pk
        }
        file_serializer = RestaurantImageSerializer(data=data)

        if file_serializer.is_valid():
            file_serializer.save()
            return Response(status=status.HTTP_201_CREATED)
        else:
            return Response(file_serializer.errors, status=status.HTTP_400_BAD_REQUEST)

the serializer

class RestaurantImageSerializer(serializers.ModelSerializer):
    class Meta:
        model = RestaurantImage
        fields = '__all__'

and model

class RestaurantImage(models.Model):
    info = models.ForeignKey(RestaurantInfo, related_name='images', on_delete=models.CASCADE)
    image = models.ImageField()

    def __str__(self):
        return self.image.name

1 Answers1

0

Your Postman configuration could be an issue, try removing all the wrong or unnecessary headers. I think you need only Authorization in your case. This also could help you out: https://stackoverflow.com/a/41435972/4907382

Dawid Bugajewski
  • 365
  • 1
  • 11