首页 > 解决方案 > 如何使用 aiohttp multipartwriter 发送图像?

问题描述

我正在尝试使用不和谐机器人将图像作为消息发送。我期待得到这样的东西但我得到

Cannot create payload from {'name': 'file', 'value': <_io.BufferedReader name='./emojis/1f92f.png'>, 'filename': '1f92f.png', 'content-type': 'application/octet-stream'}

当我使用这个时:

file = open(f'./emojis/{final_point}.png', 'rb')
writer = aiohttp.multipart.MultipartWriter('form-data', boundary='--boundary--')
writer.append({'content_type': 'application/octet-stream', 'name': f'{final_point}.png','value': file})
async with aiohttp.ClientSession() as session:
    await session.post(f'{util.url}/interactions/{interaction.id}/{interaction.token}/callback', data=writer)

[400 Bad Request]当我使用它时,我得到了:

file = open(f'./emojis/{final_point}.png', 'rb')
writer = aiohttp.multipart.MultipartWriter('form-data', boundary='--boundary--')
writer.append(file, {'CONTENT-TYPE': 'image/png'})
async with aiohttp.ClientSession() as session:
    await session.post(f'{util.url}/interactions/{interaction.id}/{interaction.token}/callback', data=writer)

有人有解决方案吗?

标签: pythondiscordaiohttp

解决方案


推荐阅读