1

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.

1 Answers1

0

It's possible that why it gives you 401 Unauthorized due the authentication process wasn't completed in the meantime. So when you add a timeout or friction of delay (like you mentioned about 5-10 seconds), the request has enough time to proceed with the authentication until it's completed, thus it will give you 200 OK

But my suggestion is, you need to check whether the request payload and the username or password that you set are correct / match with the credential.