我不会建议提交在这种情况下您的数据,JSON,因为这个问题复杂化。只需使用要提交的参数和文件进行POST
请求即可。 Django REST Framework可以很好地处理它,无需将其作为JSON序列化。
我写了一个测试文件上载到的API端点而回看起来像这样:
def test_post_photo(self):
"""
Test trying to add a photo
"""
# Create an album
album = AlbumFactory(owner=self.user)
# Log user in
self.client.login(username=self.user.username, password='password')
# Create image
image = Image.new('RGB', (100, 100))
tmp_file = tempfile.NamedTemporaryFile(suffix='.jpg')
image.save(tmp_file)
# Send data
with open(tmp_file.name, 'rb') as data:
response = self.client.post(reverse('photo-list'), {'album': 'http://testserver/api/albums/' + album.pk, 'image': data}, format='multipart')
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
在这种情况下,我使用的tempfile
模块,用于存储图像使用枕头生成。示例中使用的with
语法允许您比较容易地传递请求正文中的文件内容。
在此基础上,这样的事情应该为您的使用情况下工作:
image = Image.new('RGBA', size=(50, 50), color=(155, 0, 0))
file = tempfile.NamedTemporaryFile(suffix='.png')
image.save(file)
with open(file.name, 'rb') as data:
return self.client.post("/api/images/", {"image": data, "item": 1}, format='multipart')
顺便说一句,这取决于你使用的情况下,可以更方便地接受图像数据作为基64编码字符串。
您是否尝试省略'json.dumps'调用?在我的django项目中,我只是使用测试客户端将数据作为字典发布。 – Brobin