Reputation: 43
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 ?
Upvotes: 4
Views: 4576
Reputation: 8023
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?
For multipart form you should use the MultiPartParser
: https://www.django-rest-framework.org/api-guide/parsers/#multipartparser
Upvotes: 1