使用 aiohttp 从 Python 中的内存上传 multipart/form-data

Upload multipart/form-data from memory in Python with aiohttp

我正在尝试与 Discord.py 合作,将附件上传到 Ballchasing 的 API。以下是相关的 API 部分:

https://discordpy.readthedocs.io/en/latest/api.html#discord.Attachment.read

https://ballchasing.com/doc/api#upload-upload-post

文档中的示例建议使用请求,但我一遍又一遍地读到这不是 Discord 机器人的最佳实践,因为您希望异步代码避免任何可能阻止脚本执行的事情.

这是我的:

@commands.Cog.listener()        
async def on_message(self, message):
    headers = {'Authorization':self.upload_key_bc}
    for attachment in message.attachments:
        file = io.BytesIO(await attachment.read())
        action = {'file': ('replay.replay', file.getvalue())}
        async with aiohttp.ClientSession() as session:
            async with session.post(self.api_upload_bc, headers=headers, data=action) as response:
                print(response.status)
                print(await response.text())

我收到这样的回复:

failed to get multipart form: request Content-Type isn't multipart/form-data

我尝试将 Content-Type header 强制设置为 multiparth/form-data,但出现了不同的错误:

failed to get multipart form: no multipart boundary param in Content-Type

我认为我发送数据的方式有问题。我错过了什么?

为了将其转换为 multipart/form-data,必须将文件添加为 io.IOBase 对象,或者您必须使用带有特定修饰符的 add_field。查看 FormData 下的文档。 https://docs.aiohttp.org/en/stable/client_reference.html#aiohttp.FormData

手动创建一个 FormData 对象并明确指定文件名以启用 multipart/form-data.

如果深入研究代码,{'file': ('replay.replay', file.getvalue())}FormData 中被视为非特殊值并在 urllib.parse.urlencode() 中呈现为 str(('replay.replay', file.getvalue())

from aiohttp import FormData

@commands.Cog.listener()
async def on_message(self, message):
    headers = {'Authorization': self.upload_key_bc}
    for attachment in message.attachments:
        
        formdata = FormData()
        formdata.add_field('file', BytesIO(await attachment.read()), filename='replay.replay')

        async with aiohttp.ClientSession() as session:
            async with session.post(self.api_upload_bc, headers=headers, data=formdata) as response:
                print(response.status)
                print(await response.text())