python - AIOHTTP 在调用 raise_for_status 时具有请求正文/内容/文本
问题描述
我正在使用FastAPI
with aiohttp
,我为持久会话构建了一个单例,并使用它在启动时打开会话并在关闭时关闭它。
需求:response
身体很珍贵,万一发生故障,我必须把它和其他细节一起记录下来。
因为raise_for_status
我必须编写处理每个 HTTP 方法的那些丑陋的函数,这就是其中之一:
async def post(self, url: str, json: dict, headers: dict) -> ClientResponse:
response = await self.session.post(url=url, json=json, headers=headers)
response_body = await response.text()
try:
response.raise_for_status()
except Exception:
logger.exception('Request failed',
extra={'url': url, 'json': json, 'headers': headers, 'body': response_body})
raise
return response
如果我可以指望raise_for_status
返回正文(response.text()
),我可以启动会话ClientSession(raise_for_status=True)
并编写一个干净的代码:
response = await self.session.post(url=url, json=json, headers=headers)
有没有办法强制以某种方式raise_for_status
返回有效载荷/主体,也许在初始化ClientSession
?
谢谢您的帮助。
解决方案
aiohttp
和是不可能的raise_for_status
。正如@Andrew Svetlov在这里回答的那样:
引发异常后将响应视为关闭。从技术上讲,它可以包含部分主体,但没有任何保证。没有理由阅读它,主体可能非常巨大,1GiB 不是限制。如果您需要非 200 的响应内容,请明确阅读。
或者,考虑以这种方式使用httpx库。(它与 FastAPI 一起广泛使用):
def raise_on_4xx_5xx(response):
response.raise_for_status()
async with httpx.AsyncClient(event_hooks={'response': [raise_on_4xx_5xx]}) as client:
try:
r = await client.get('http://httpbin.org/status/418')
except httpx.HTTPStatusError as e:
print(e.response.text)
推荐阅读
- windows - 为什么从与网络浏览器 Lq350 epson 不同的 PDF 程序打印
- apache - 通过局域网从另一台计算机访问本地主机(wamp)的问题
- python - DiscordPy 禁用命令:在特定服务器中禁用特定命令的命令
- javascript - js格式的PHP回显数组
- html - 为所有具有相同属性的元素添加通用 CSS
- c# - 如何从用户控件 WPF C# 访问父窗口中的按钮
- loops - 批处理文件:计算重复的 id 并将它们写入 csv 列
- reactjs - 在功能组件中使用 React.memo 来 renderItem FlatList 以最小化重新渲染现有项目
- c++ - 对 C++17 Filesystem::last_write_time 的向量进行排序的问题
- javascript - 角度上传 - 无法查看旧数据