Skip to content

httpx 0.28.1 raises InvalidURL 'Invalid port' when NO_PROXY contains ::1 or [::1]

A Python CLI using httpx 0.28.1 (httpx.AsyncClient) failed every API call inside a sandbox whose egress goes through an HTTP proxy. The sandbox exports HTTPS_PROXY=http://127.0.0.1:3128 and NO_PROXY=127.0.0.1,localhost,::1,[::1] (lowercase too). The error came at client construction, before any request: httpx.InvalidURL: Invalid port: ':1]'. curl, gh and Go tools in the same environment used the same proxy variables without complaint. Removing only the bare ::1 entry did not help.

1 solution
ranked by outcome — not votes
Accepted

httpx builds a proxy mount for each NO_PROXY entry when the client is constructed, and it can't parse an IPv6 loopback entry in either form: ::1 and [::1] both raise InvalidURL: Invalid port: ':1]' (httpx 0.28.1, Python 3.12). Reproduce:

import os, httpx
os.environ.update(NO_PROXY='127.0.0.1,localhost,[::1]', HTTPS_PROXY='http://127.0.0.1:3128')
httpx.AsyncClient()  # InvalidURL: Invalid port: ':1]'

NO_PROXY=127.0.0.1,localhost works. If you can't change the environment that sets it (a sandbox, CI image, corporate profile), strip the IPv6 entries in a launcher before the Python process starts:

for v in NO_PROXY no_proxy; do
    eval "cur=\${$v:-}"
    [ -n "$cur" ] && export "$v=$(printf '%s' "$cur" | tr ',' '\n' | grep -vxE '::1|\[::1\]' | paste -sd, -)"
done

or pass trust_env=False plus an explicit proxy= to the client if you own the code.