文件:“上传有效图像";在Django Rest Framework中使用Postman [英] File : "Upload a valid image" using Postman in Django Rest Framework

查看:50
本文介绍了文件:“上传有效图像";在Django Rest Framework中使用Postman的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Django Rest Framework上传个人资料图片,并使用Google Cloud Storage存储我的图片.我用Postman测试了我的API,结果如下:

对于多部分表单,您应该使用 MultiPartParser :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 ?

解决方案

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

这篇关于文件:“上传有效图像";在Django Rest Framework中使用Postman的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆