Allow for tracing headers sent on the wire by the client #5105
Description
🐣 Is your feature request related to a problem? Please describe.
Feature request:
TraceConfig
does not allow for tracing "headers sent" stage, while it is quite often useful to verify them on-the-wire.
💡 Describe the solution you'd like
Implement on_headers_sent signal which provides exact on-the wire headers buffer sent, similar to on_request_chunk_sent.
The implementation should be quite straightforward:
- Define a new signal in
TraceConfig
- Provide the callback as
on_headers_sent
toStreamWriter
constructor, similar likeon_chunk_sent
is provided - Call the callback in write_headers, providing the serialized
buf
.
❓ Describe alternatives you've considered
Workaround to see on-the-wire headers seems to be possible only with unittest.patch
, or with monkey-copying whole ClientRequest.send
lenghty function into a child class, adding logging there and providing this class to ClientSession
.
Here is a sample workaround with patching:
class StreamWriter(aiohttp.http_writer.StreamWriter):
async def write_headers(self, status_line: str, headers) -> None:
buf = aiohttp._http_writer._serialize_headers(status_line, headers)
self._write(buf)
logging.debug(f'Wrote headers\n:{buf.decode("utf-8")}')
patch('aiohttp.client_reqrep.StreamWriter', StreamWriter).start()
📋 Additional context
When performing some API calls to remote server requiring very specific or complicated headers, it is useful to compare exact headers sent to e.g. what is sent through postman or browser. Not having access to textual representation of headers make this tedious. Also such a feature would allow for validating if cookie headers are correctly set by aiohttp.ClientSession
.