Pytest Image Upload - Error rest_framework.exceptions.ParseError: Multipart form parse error - Invalid boundary in Multipart

Pytest Image Upload - Error rest_framework.exceptions.ParseError: Multipart form parse error - Invalid boundary in Multipart

我正在测试将图像上传到用户个人资料的 API 调用。 returns 状态代码 400 下面的测试用例。如果我从数据中删除 image_filename 键,测试用例就会成功。如何在 pytest 中测试图片上传?

def test_edit_user_profile(db, client):    
         # stream the image into binary
         with open('C:/...../test.png', 'rb') as consultant_image:
                 image_binary = BytesIO(consultant_image.read())

         userdetail_response = client.patch(path=reverse('user-detail', args="1"),
                                        data={"full_name": "James edited",
                                              "image_filename": (image_binary, 'test2.png')},
                                        content_type='multipart/form-data',
                                        HTTP_AUTHORIZATION='Token ' + data.json()['token_key'])

         assert userdetail_response.status_code == status.HTTP_200_OK

这个解决方案有效

from django.test.client import BOUNDARY, MULTIPART_CONTENT, encode_multipart

    userdetail_response2 = client.patch(
        path=reverse('user-detail', args=(str(registered_response.json()['id']),)),
        data=encode_multipart(BOUNDARY,
                            {
                                "full_name": "James",
                                "bio": "Details about me",
                                "is_allow_direct_order": False,
                                'image_filename': open((os.path.dirname(os.path.abspath(__file__))) + '/test.png', 'rb'),
                            }),
        content_type=MULTIPART_CONTENT,
        HTTP_AUTHORIZATION='Token ' + registered_response.json()['token_key']
    )