I'm using async with httpx in a python script to make an HTTP POST request using DigestAuth.
async with httpx.AsyncClient() as client:
try:
r = await client.post(f"http://{str(ip)}{setConfAddr}", auth=httpx.DigestAuth(username, password), data=confPayload, timeout=10)
logging.info(f"{str(ip)} - {r.content}")
except httpx.RequestError as e:
sys.exit(f"post configuration failed: {e}")
This was failing, giving a 401 error until I introduced the timeout, which solved the issue I thought - but looking at the logs, I get a failure followed by success, every time:
2023-02-21 06:54:43,347 HTTP Request: POST http://10.0.0.17/cgi-bin/set_conf.cgi "HTTP/1.1 401 Unauthorized"
2023-02-21 06:55:05,267 HTTP Request: POST http://10.0.0.17/cgi-bin/set_conf.cgi "HTTP/1.1 200 OK"
I added a timeout which I thought solved the issue. Apparently it just gave it more time to retry and succeed. The result is a very slow (5-10s) delay for a successful result.
I'd like to get to the root cause of why it fails with a 401 error initially, but apparently works after a retry. I suspect it's some way in which it is doing the authorisation too quickly for the host. I'm unsure how to troubleshoot this from here.