utf8' codec can't decode byte 0x89 in position 15: invalid start byte

对着背影说爱祢 提交于 2019-12-20 06:16:47

问题


This is a view for my project in which I am trying to upload images from my local system to s3 using boto.

class ImageList(generics.ListCreateAPIView):
        queryset = Image.objects.all()
        serializer_class = ImageSerializer

        def post(self , request , format = None):
            # import ipdb; ipdb.set_trace()
            serializer = ImageSerializer(data = request.data)
            if serializer.is_valid():
                serializer.save()
                print request.data
                return Response({'received data' : request.data})
            return Response(serializer.errors , status = status.HTTP_400_BAD_REQUEST)

class ImageDetail(generics.RetrieveUpdateDestroyAPIView):
    queryset = Image.objects.all()
    serializer_class = ImageSerializer

It works fine for django admin but generates error while I try to upload it using Django Rest Framework html form. I have overridden the post method in generic class based view. The error that gets generated is :

UnicodeDecodeError at /image/
'utf8' codec can't decode byte 0xff in position 15: invalid start byte
Request Method: POST
Request URL:    http://127.0.0.1:8000/image/
Django Version: 1.9
Exception Type: UnicodeDecodeError
Exception Value:    
'utf8' codec can't decode byte 0xff in position 15: invalid start byte
Exception Location: /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/encoder.py in encode, line 210
Python Executable:  /usr/bin/python
Python Version: 2.7.10
Unicode error hint

The string that could not be encoded/decoded was: "����\u

I have tried almost everything available on stack overflow but nothing worked for me. I didn't copy the code from somewhere so there would be no strange characters.

For reference this is the models.py file:

class Image(models.Model):
    image_meta = models.ForeignKey('Image_Meta',on_delete=models.CASCADE,)
    image = models.ImageField(upload_to='images-data')
    # image = models.URLField(max_length = 500)
    order = models.IntegerField()
    version = models.CharField(max_length=10)

    def __unicode__(self):
            return (self.image)

See this for more detail :


回答1:


JSON strings are unicode strings, not binary strings. Your image contains binary data, and the JSON serializer is complaining about that.

One common approach to work around this problem is to use base64 or base85. Python has builtin support for those two (and other) encodings via the base64 module.



来源:https://stackoverflow.com/questions/35953939/utf8-codec-cant-decode-byte-0x89-in-position-15-invalid-start-byte

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!