Skip to content

Commit c6eb530

Browse files
Fix content-type on empty string. (#5393)
Co-authored-by: Andrew Svetlov <[email protected]>
1 parent c091db8 commit c6eb530

File tree

3 files changed

+23
-3
lines changed

3 files changed

+23
-3
lines changed

CHANGES/5392.bugfix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Set "text/plain" when data is an empty string in client requests.

aiohttp/client_reqrep.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ def __init__(
245245
self.update_proxy(proxy, proxy_auth, proxy_headers)
246246

247247
self.update_body_from_data(data)
248-
if data or self.method not in self.GET_METHODS:
248+
if data is not None or self.method not in self.GET_METHODS:
249249
self.update_transfer_encoding()
250250
self.update_expect_continue(expect100)
251251
if traces is None:
@@ -383,7 +383,7 @@ def update_cookies(self, cookies: Optional[LooseCookies]) -> None:
383383

384384
def update_content_encoding(self, data: Any) -> None:
385385
"""Set request content encoding."""
386-
if not data:
386+
if data is None:
387387
return
388388

389389
enc = self.headers.get(hdrs.CONTENT_ENCODING, "").lower()
@@ -433,7 +433,7 @@ def update_auth(self, auth: Optional[BasicAuth]) -> None:
433433
self.headers[hdrs.AUTHORIZATION] = auth.encode()
434434

435435
def update_body_from_data(self, body: Any) -> None:
436-
if not body:
436+
if body is None:
437437
return
438438

439439
# FormData

tests/test_client_functional.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3022,3 +3022,22 @@ async def handler(request):
30223022

30233023
async with await client.get("/", read_bufsize=4) as resp:
30243024
assert resp.content.get_read_buffer_limits() == (4, 8)
3025+
3026+
3027+
async def test_http_empty_data_text(aiohttp_client: Any) -> None:
3028+
async def handler(request):
3029+
data = await request.read()
3030+
ret = "ok" if data == b"" else "fail"
3031+
resp = web.Response(text=ret)
3032+
resp.headers["Content-Type"] = request.headers["Content-Type"]
3033+
return resp
3034+
3035+
app = web.Application()
3036+
app.add_routes([web.post("/", handler)])
3037+
3038+
client = await aiohttp_client(app)
3039+
3040+
async with await client.post("/", data="") as resp:
3041+
assert resp.status == 200
3042+
assert await resp.text() == "ok"
3043+
assert resp.headers["Content-Type"] == "text/plain; charset=utf-8"

0 commit comments

Comments
 (0)