I want to display all the HTTP headers (the ones I added and the auto generated) of a request. I tried using traces (https://aiohttp.readthedocs.io/en/stable/tracing_reference.html#aiohttp-client-tracing-reference) :
#!/usr/bin/env python3
import aiohttp
import asyncio
async def on_request_start(session, trace_config_ctx, params):
print("Starting %s request for %s. I will send: %s" % (params.method, params.url, params.headers))
async def on_request_end(session, trace_config_ctx, params):
print("Ending %s request for %s. I sent: %s" % (params.method, params.url, params.headers))
async def fetch(session, url):
async with session.get(url) as response:
return response
async def main():
trace_config = aiohttp.TraceConfig()
trace_config.on_request_start.append(on_request_start)
trace_config.on_request_end.append(on_request_end)
async with aiohttp.ClientSession(trace_configs=[trace_config]) as session:
r = await fetch(session, 'http://stackoverflow.com')
print(r)
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
With this code, I get the method and the URL but the dict of headers is always empty:
% ./test-debug.py
Starting GET request for http://stackoverflow.com. I will send: <CIMultiDict()>
Ending GET request for https://stackoverflow.com/. I sent: <CIMultiDict()>
What did I miss?
Python 3.7.2
% pip show aiohttp
Name: aiohttp
Version: 3.5.4
Summary: Async http client/server framework (asyncio)
Home-page: https://github.com/aio-libs/aiohttp
Author: Nikolay Kim
Author-email: fafhrd91@gmail.com
License: Apache 2
Location: /usr/lib/python3.7/site-packages
Requires: async-timeout, attrs, multidict, yarl, chardet
Required-by: