4

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 ?

0

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you, i will try this today and tell you if it works !
Thank you very much, it works with binary file in Postman ! Finally I use multipart form with MultiPartParser because it works and it is easier to send request with axios. Thank you again !
Glad it helped!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.