繁体   English   中英

如何使用 Python+aiohttp 获取 HTTP 403 响应的正文?

[英]How do I get the body of an HTTP 403 response using Python+aiohttp?

我正在使用 Python 3.6 和 aiohttp 库向服务器发出 API Post 请求。 如果我在发出请求时使用了错误的用户名,就会如我所料地收到 HTTP 403 错误。 当我在 Postman 中发出此请求时,响应正文显示:

{"error_message": "No entitlements for User123"}

但是,当我使用 aiohttp 发出请求时,我在任何地方都看不到此响应正文。 该消息只是说“禁止”。 我怎样才能在我的 Python 代码中得到上面的错误信息?

编辑:这是我的 aiohttp 代码,虽然它很简单:

try:
    async with self.client_session.post(url, json=my_data, headers=my_headers) as response:
        return await response.json()
except ClientResponseError as e:
    print(e.message)  # I want to access the response body here
    raise e

编辑 2:我找到了一个解决方法。 当我创建client_session ,我将 raise_for_status 值设置为 False。 然后当我从 API 调用得到响应时,我检查状态是否 >= 400。如果是,我自己处理错误,其中包括响应的正文。

编辑 3:这是我的解决方法的代码:

self.client_session = ClientSession(loop=asyncio.get_event_loop(), raise_for_status=False)
####################### turn off the default exception handling ---^

try:
    async with self.client_session.post(url, json=my_data, headers=my_headers) as response:
    body = await response.text()

    # handle the error myself so that I have access to the response text
    if response.status >= 400:
        print('Error is %s' % body)
        self.handle_error(response)

是的,如果您来自具有.request.response (或其他方式)属性的异常对象的requests包,这可能确实令人困惑。

您已经很明显地想到了这一点,但这里有一个适用于旧版本aiohttp 的正确答案。

async with session.post(...) as response:
    try:
        response.raise_for_status()
    except ClientResponseError as err:
        logger.error("Error: %s, Error body: %s", err, (await response.text()))

    return await response.json()

不幸的是,一旦raise_for_status() ,较新的版本就会回收连接,因此您以后无法获取错误正文。 这是现在对我有用的东西(来自http-noah包):

        logger = structlog.get_logger(__name__)

        async with session.post(url, **req_kwargs) as res:
            # Fetching text as error just in case - raise_for_status() will
            # release the connection so the error body will be lost already.
            # The text will be cached in the response internally for the use later on
            # so no waste here.
            err_body = await res.text()
            try:
                res.raise_for_status()
            except aiohttp.ClientResponseError as err:
                logger.error("Request failed", err=err, err_body=err_body)
                raise

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM