Aiohttp如何记录请求体

pha*_*der 9 python-3.x aiohttp

我正在使用 aiohttp 并希望在发送原始请求之前将其记录在 ClientSession 中。我看到对于 requests 模块,它是可行的:Python requests - print another http request (raw)?

但是当我尝试查找 aiohttp 时,我只能找到请求标头,但我想要的是正文:Dumping the request headers with aiohttp

我已经查看了跟踪的源代码,但我所能找到的只是正在使用的 ClientResponse 类,并且没有使用 ClientRequest 类的实例: https: //github.com/aio-libs/aiohttp/blob/master/aiohttp/tracing .py

Ema*_*ber 0

您可以使用跟踪检查请求正文,如下所示:

async def on_request_chunk_sent(session, trace_config_ctx, chunk):
        print("Chunk sent request", chunk)

trace_config = aiohttp.TraceConfig()
trace_config.on_request_chunk_sent.append(on_request_chunk_sent)
async with aiohttp.ClientSession(trace_configs=[trace_config]) as session:
...
Run Code Online (Sandbox Code Playgroud)

有关更多信息,请参阅文档