From 324277091cbad502e3e85e11af9819c739973dea Mon Sep 17 00:00:00 2001 From: Jigsaw Date: Sun, 11 Jun 2023 06:59:05 +0000 Subject: [PATCH] =?UTF-8?q?:bug:=20Fix:=20aiohttp=20=E8=AF=B7=E6=B1=82?= =?UTF-8?q?=E6=97=B6=20data=20=E5=92=8C=20file=20=E4=B8=8D=E8=83=BD?= =?UTF-8?q?=E5=90=8C=E6=97=B6=E5=AD=98=E5=9C=A8=20(#2088)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- nonebot/drivers/aiohttp.py | 9 +++++---- tests/test_driver.py | 6 +++++- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/nonebot/drivers/aiohttp.py b/nonebot/drivers/aiohttp.py index c24bb972..a4720af6 100644 --- a/nonebot/drivers/aiohttp.py +++ b/nonebot/drivers/aiohttp.py @@ -51,11 +51,12 @@ class Mixin(ForwardMixin): raise RuntimeError(f"Unsupported HTTP version: {setup.version}") timeout = aiohttp.ClientTimeout(setup.timeout) - files = None + + data = setup.data if setup.files: - files = aiohttp.FormData() + data = aiohttp.FormData(data or {}) for name, file in setup.files: - files.add_field(name, file[1], content_type=file[2], filename=file[0]) + data.add_field(name, file[1], content_type=file[2], filename=file[0]) cookies = { cookie.name: cookie.value for cookie in setup.cookies if cookie.value @@ -66,7 +67,7 @@ class Mixin(ForwardMixin): async with session.request( setup.method, setup.url, - data=setup.content or setup.data or files, + data=setup.content or data, json=setup.json, headers=setup.headers, timeout=timeout, diff --git a/tests/test_driver.py b/tests/test_driver.py index 624cf00e..48b503a0 100644 --- a/tests/test_driver.py +++ b/tests/test_driver.py @@ -177,11 +177,15 @@ async def test_http_driver(driver: Driver): assert data["json"] == {"json": "test"} request = Request( - "POST", "https://httpbin.org/post", files={"test": ("test.txt", b"test")} + "POST", + "https://httpbin.org/post", + data={"form": "test"}, + files={"test": ("test.txt", b"test")}, ) response = await driver.request(request) assert response.status_code == 200 and response.content data = json.loads(response.content) + assert data["form"] == {"form": "test"} assert data["files"] == {"test": "test"} await asyncio.sleep(1)