Python httpx 记录所有请求标头

问题描述 投票:0回答:1

如何记录 httpx 请求的所有请求标头?我使用日志级别 DEBUG nad 响应标头日志正常,但日志中没有请求标头。如果重要的话,我正在使用 httpx lib 的异步 api。

python httpx
1个回答
0
投票

使用自定义事件挂钩:

import httpx
import logging

logging.basicConfig(level=logging.DEBUG)
async def log_request(request):
    logging.debug(f"Request headers: {request.headers}")
async with httpx.AsyncClient(event_hooks={'request': [log_request]}) as client:
    response = await client.get('https://httpbin.org/get')
© www.soinside.com 2019 - 2024. All rights reserved.