Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

File : "Upload a valid image" using Postman in Django Rest Framework

I am using Django Rest Framework to upload profile picture and Google Cloud Storage to store my images. I test my API with Postman and i have this result : Postman result And here is my Postman headers : Postman headers

This is my code :

class ProfilePictureSerializer(serializers.Serializer):
file = serializers.ImageField()

class UploadProfilePictureAPI(APIView):
permission_classes = (IsAuthenticated,)
parser_classes = [FileUploadParser]

@staticmethod
def post(request):
    input_serializer = serializers.ProfilePictureSerializer(
        data=request.data
    )
    input_serializer.is_valid(raise_exception=True)

    profile = ProfileService.upload_profile_picture(
        request.user,
        **input_serializer.validated_data
    )

    output_serializer = serializers.ProfileSerializer(profile)
    return Response(
        output_serializer.data,
        status=status.HTTP_202_ACCEPTED
    )

@staticmethod
def upload_profile_picture(user, file):
    user.profile.profile_picture = file
    user.profile.save()
    return user.profile

path(
    'upload/picture',
    views.UploadProfilePictureAPI.as_view(),
    name='api_upload_profile_picture'
),

I does not understand why I have this response. Could you help me ?

like image 370
Nicod44 Avatar asked Dec 05 '25 05:12

Nicod44


1 Answers

I think the issue here might be that FileUploadParser expects you to just send raw binary data (you can see the binary option in Postman).

In your current example, can you just try to send the binary data like so?

enter image description here

For multipart form you should use the MultiPartParser: https://www.django-rest-framework.org/api-guide/parsers/#multipartparser

like image 85
inostia Avatar answered Dec 07 '25 20:12

inostia



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!