-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Can't read response content after checking response.ok #5403
Copy link
Copy link
Closed
Labels
bugclientreproducer: presentThis PR or issue contains code, which reproduce the problem described or clearly understandable STRThis PR or issue contains code, which reproduce the problem described or clearly understandable STR
Description
🐞 Describe the bug
For failed requests, ClientResponse is "released" after calling to resposne.ok and therefore can't read response.text() after checking response.ok
💡 To Reproduce
async with session.get("url-returning-400+") as response:
if not response.ok:
msg = await response.text() # raises ClientConnectionError('Connection closed')does not work, while
async with session.get("url-returning-400+") as response:
if response.status != 200:
msg = await response.text() # works just fineworks just fine
💡 Expected behavior
I'm not sure this is expected behaviour. response.ok is read-only property and I believe it should not change the state of the response object. Also, failed requests may contain response body, e.g. with error description, so I'm expecting await response.text() to work properly after checking response.ok
📋 Your version of the Python
$ python --version
Python 3.8.6📋 Your version of the aiohttp/yarl/multidict distributions
$ python -m pip show aiohttp
Name: aiohttp
Version: 3.7.3
Summary: Async http client/server framework (asyncio)
Home-page: https://github.com/aio-libs/aiohttp
Author: Nikolay Kim
Author-email: [email protected]
License: Apache 2
Location: /Users/I028910/sbn/job-context-store/.venv/lib/python3.8/site-packages
Requires: async-timeout, attrs, typing-extensions, chardet, yarl, multidict
Required-by: sbn-aio-utils, aioresponses, aiohttp-retry$ python -m pip show multidict
Name: multidict
Version: 5.1.0
Summary: multidict implementation
Home-page: https://github.com/aio-libs/multidict
Author: Andrew Svetlov
Author-email: [email protected]
License: Apache 2
Location: /Users/I028910/sbn/job-context-store/.venv/lib/python3.8/site-packages
Requires:
Required-by: yarl, aiohttp$ python -m pip show yarl
Name: yarl
Version: 1.6.3
Summary: Yet another URL library
Home-page: https://github.com/aio-libs/yarl/
Author: Andrew Svetlov
Author-email: [email protected]
License: Apache 2
Location: /Users/I028910/sbn/job-context-store/.venv/lib/python3.8/site-packages
Requires: idna, multidict
Required-by: aiohttpReactions are currently unavailable
Metadata
Metadata
Assignees
Labels
bugclientreproducer: presentThis PR or issue contains code, which reproduce the problem described or clearly understandable STRThis PR or issue contains code, which reproduce the problem described or clearly understandable STR