Aiohttp how to log request body
Asked Answered
T

1

10

I'm using aiohttp and want to log the raw request in ClientSession before it's sent. I saw that for requests module, it is doable: Python requests - print entire http request (raw)?

But when I tried finding for aiohttp, I can only find for request headers, but what I want is the body: Dumping the request headers with aiohttp

I have looked into the source code for tracing but all I can find is the ClientResponse class being used and no instances of ClientRequest class being used: https://github.com/aio-libs/aiohttp/blob/master/aiohttp/tracing.py

Tuggle answered 1/7, 2020 at 11:3 Comment(0)
F
1

You can inspect the request body using tracing as the following:

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:
...

For more info, refer to the docs.

Faefaeces answered 17/1 at 11:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.