🐛 Fix: aiohttp 请求时 data 和 file 不能同时存在 (#2088)

This commit is contained in:
Jigsaw 2023-06-11 06:59:05 +00:00 committed by GitHub
parent 6eef863b70
commit 324277091c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 10 additions and 5 deletions

View File

@ -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,

View File

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