Skip to content

Commit

Permalink
[PR #5393/c6eb5307 backport][3.8] Fix content-type on empty string. (#…
Browse files Browse the repository at this point in the history
…6121)

* Fix content-type on empty string. (#5393)

Co-authored-by: Andrew Svetlov <[email protected]>
(cherry picked from commit c6eb530)

* fix

Co-authored-by: Sam Bull <[email protected]>
Co-authored-by: Andrew Svetlov <[email protected]>
  • Loading branch information
3 people authored Oct 24, 2021
1 parent a4b7505 commit 8d66590
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGES/5392.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Set "text/plain" when data is an empty string in client requests.
6 changes: 3 additions & 3 deletions aiohttp/client_reqrep.py
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ def __init__(
self.update_proxy(proxy, proxy_auth, proxy_headers)

self.update_body_from_data(data)
if data or self.method not in self.GET_METHODS:
if data is not None or self.method not in self.GET_METHODS:
self.update_transfer_encoding()
self.update_expect_continue(expect100)
if traces is None:
Expand Down Expand Up @@ -449,7 +449,7 @@ def update_cookies(self, cookies: Optional[LooseCookies]) -> None:

def update_content_encoding(self, data: Any) -> None:
"""Set request content encoding."""
if not data:
if data is None:
return

enc = self.headers.get(hdrs.CONTENT_ENCODING, "").lower()
Expand Down Expand Up @@ -499,7 +499,7 @@ def update_auth(self, auth: Optional[BasicAuth]) -> None:
self.headers[hdrs.AUTHORIZATION] = auth.encode()

def update_body_from_data(self, body: Any) -> None:
if not body:
if body is None:
return

# FormData
Expand Down
19 changes: 19 additions & 0 deletions tests/test_client_functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -3007,3 +3007,22 @@ async def handler(request):

async with await client.get("/", read_bufsize=4) as resp:
assert resp.content.get_read_buffer_limits() == (4, 8)


async def test_http_empty_data_text(aiohttp_client) -> None:
async def handler(request):
data = await request.read()
ret = "ok" if data == b"" else "fail"
resp = web.Response(text=ret)
resp.headers["Content-Type"] = request.headers["Content-Type"]
return resp

app = web.Application()
app.add_routes([web.post("/", handler)])

client = await aiohttp_client(app)

async with await client.post("/", data="") as resp:
assert resp.status == 200
assert await resp.text() == "ok"
assert resp.headers["Content-Type"] == "text/plain; charset=utf-8"

0 comments on commit 8d66590

Please sign in to comment.