Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python httpx log all request headers

Tags:

python

httpx

How can I log all request headers of an httpx request? I use log level DEBUG nad response headers log fine but there are no request headers in the log. If it matters I'm using async api of httpx lib.

like image 773
Grigory Avatar asked Dec 28 '25 14:12

Grigory


1 Answers

Use custom event hooks:

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')
like image 189
Laszlo Hunyadi Avatar answered Dec 30 '25 21:12

Laszlo Hunyadi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!