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

dnL*_*nLL 4 python multipartform-data aiohttp discord.py

我正在尝试使用 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())
Run Code Online (Sandbox Code Playgroud)

我收到这样的回复:

failed to get multipart form: request Content-Type isn't multipart/form-data
Run Code Online (Sandbox Code Playgroud)

我尝试将 Content-Type 标头强制为 multipath/form-data,但出现了不同的错误:

failed to get multipart form: no multipart boundary param in Content-Type
Run Code Online (Sandbox Code Playgroud)

我认为我发送数据的方式有问题。我缺少什么?

Aar*_*ron 10

手动创建一个FormData对象并显式指定文件名以启用多部分/表单数据。

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

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())

Run Code Online (Sandbox Code Playgroud)