UnicodeDecodeError在/api/image/处发生的原因是什么?

huangapple go评论53阅读模式
英文:

Why coming this error: UnicodeDecodeError at /api/image/

问题

我的目标是在响应中显示图像的URL,但它不起作用。我遇到了错误。这里实际上是否可以返回 request.data.get('img')?你能否给我一个解决方案?

serializer.py:

class ImageSerializer(serializers.Serializer):
    img = serializers.ImageField(required=False)

views.py:

class ImageView(APIView):
    def post(self, request):
        serializer = ImageSerializer(data=request.data)
        return Response(
            {
                "processed_image_data": request.data.get('img')
            }, 
            status=status.HTTP_200_OK
        )

errors:

UnicodeDecodeError at /api/image/
'utf-8' codec can't decode byte 0xff in position 0: invalid start byte
...
英文:

My goal is to show the image's URL in response but it's not working. I getting errors. Here actually is it possible to return request.data.get('img')? Can you please give me a solution?

serializer.py:

class ImageSerializer(serializers.Serializer):
    img = serializers.ImageField(required=False)

views.py:

class ImageView(APIView):
   def post(self, request):
        serializer = ImageSerializer(data=request.data)
        return Response(
            {
                "processed_image_data": request.data.get('img')
            }, 
            status=status.HTTP_200_OK
        )

errors:

UnicodeDecodeError at /api/image/
'utf-8' codec can't decode byte 0xff in position 0: invalid start byte
Request Method:	POST
Request URL:	http://127.0.0.1:8000/api/image/
Django Version:	4.2
Exception Type:	UnicodeDecodeError
Exception Value:	
'utf-8' codec can't decode byte 0xff in position 0: invalid start byte
Exception Location:	D:_Spondon-Bhai_Image-Process\venv\lib\site-packages\rest_framework\utils\encoders.py, line 50, in default
Raised during:	app.views.ImageView
Python Executable:	D:_Spondon-Bhai_Image-Process\venv\Scripts\python.exe
Python Version:	3.9.5
Python Path:	
['D:\_Spondon-Bhai\_Image-Process',
 'c:\\users\\dcl\\appdata\\local\\programs\\python\\python39\\python39.zip',
 'c:\\users\\dcl\\appdata\\local\\programs\\python\\python39\\DLLs',
 'c:\\users\\dcl\\appdata\\local\\programs\\python\\python39\\lib',
 'c:\\users\\dcl\\appdata\\local\\programs\\python\\python39',
 'D:\_Spondon-Bhai\_Image-Process\\venv',
 'D:\_Spondon-Bhai\_Image-Process\\venv\\lib\\site-packages']
Server time:	Wed, 19 Apr 2023 17:23:42 +0000
Unicode error hint
The string that could not be encoded/decoded was: ����

Traceback Switch to copy-and-paste view
D:_Spondon-Bhai_Image-Process\venv\lib\site-packages\django\core\handlers\exception.py, line 55, in inner
        return inner
    else:
        @wraps(get_response)
        def inner(request):
            try:
                response = get_response(request) …
            except Exception as exc:
                response = response_for_exception(request, exc)
            return response
        return inner
Local vars
D:_Spondon-Bhai_Image-Process\venv\lib\site-packages\django\core\handlers\base.py, line 220, in _get_response
                self.check_response(
                    response,
                    middleware_method,
                    name="%s.process_template_response"
                    % (middleware_method.__self__.__class__.__name__,),
                )
            try:
                response = response.render() …
            except Exception as e:
                response = self.process_exception_by_middleware(e, request)
                if response is None:
                    raise
        return response
Local vars

答案1

得分: 1

你遇到的问题是因为响应数据包含二进制图像数据,无法直接包含在JSON响应中。相反,你应该将图像数据转换为可包含在JSON响应中的格式,如base64。你可以尝试以下方式:

import base64
from rest_framework.parsers import MultiPartParser

class ImageView(APIView):
    parser_classes = (MultiPartParser,)

    def post(self, request):
        serializer = ImageSerializer(data=request.data)
        if serializer.is_valid():
            img = serializer.validated_data.get('img')
            img_format = img.image.format.lower()
            img_data = img.read()
            base64_img_data = base64.b64encode(img_data).decode('utf-8')
            return Response(
                {
                    "processed_image_data": f"data:image/{img_format};base64,{base64_img_data}"
                }, 
                status=status.HTTP_200_OK
            )
        else:
            return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
英文:

The issue you're encountering is because the response data contains binary image data, which cannot be directly included in a JSON response. Instead, you should convert the image data to a format that can be included in a JSON response, such as base64. You can try something as follows

import base64
from rest_framework.parsers import MultiPartParser

class ImageView(APIView):
    parser_classes = (MultiPartParser,)

    def post(self, request):
        serializer = ImageSerializer(data=request.data)
        if serializer.is_valid():
            img = serializer.validated_data.get('img')
            img_format = img.image.format.lower()
            img_data = img.read()
            base64_img_data = base64.b64encode(img_data).decode('utf-8')
            return Response(
                {
                    "processed_image_data": f"data:image/{img_format};base64,{base64_img_data}"
                }, 
                status=status.HTTP_200_OK
            )
        else:
            return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

huangapple
  • 本文由 发表于 2023年4月20日 01:00:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/76057093.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定