Proxy password with special characters
An @, :, # or / in a proxy password breaks the URL it sits in. Percent-encode the login, or hand it over in a separate field.
In short
Encode the username and password before you build http://user:pass@host:port: @ becomes %40, : %3A, # %23, / %2F, % %25. Or skip the URL and pass the login in a separate option. Two exceptions below: curl’s -U and axios’ HTTP_PROXY.
A proxy URL packs four things into one string: scheme://user:password@host:port. The parser finds them by their separators. Put @ or : inside the password and the separators no longer mean what you meant.
For the password p@ss:w0rd#1%2F, every parser we tried ended the login at the first @. What followed, ss:w0rd, became host and port, and w0rd is not a number. The # starts a fragment, so the rest of the URL disappears before anyone looks at the real host. The %2F is the quiet one: a parser that decodes the login turns it into /, and the proxy gets the wrong password without any parse error.
Percent-encoding the login takes the separators out of it. The parser splits the URL correctly, then decodes %40 back to @ before it sends Proxy-Authorization.
We gave a local Squid proxy the user alice with the password p@ss:w0rd#1%2F and called it from each tool three ways: the password as is in the URL, percent-encoded in the URL, and through the tool’s own login option.
| Tool | Raw in URL | Encoded in URL | Separate login |
|---|---|---|---|
| curl 8.23 | (5) Unsupported proxy syntax … Port number was not a decimal number between 0 and 65535 | 200 | -U: 407 (decodes %2F) |
| requests 2.34.2 | InvalidURL: Failed to parse: 'ss:w0rd' is not a valid host or port | 200 | none |
| httpx 0.28.1 | InvalidURL: Invalid port: 'w0rd' | 200 | Proxy(auth=…): 200 |
| aiohttp 3.14.5 | InvalidURL | 200 | proxy_auth: 200 |
| undici 8.12.4 (Node 24) | TypeError: Invalid URL | 200 | token: 200 |
| axios 1.21.0 | HTTP_PROXY: Invalid URL | HTTP_PROXY: 407 | proxy.auth: 200 |
| Go 1.26 net/http | invalid port ":w0rd" after host | 200 | url.UserPassword: 200 |
A parse error names your password
Look at the messages: 'ss:w0rd', 'w0rd'. They quote pieces of the password, and in a log they are part of your secret. If you see a host or port in an error that you never typed, it came from your login.
The encoding is the same everywhere
Every tool that failed on the raw URL accepted the encoded one, except axios. The encoding is standard URL encoding of the user-info part, so quote() output works in curl and Go, and encodeURIComponent() output works in Python.
curl -U decodes percent signs
-U alice:p@ss:w0rd#1%2F looks safe, since there is no URL to break. But curl decoded %2F to / before sending, and Squid answered 407. In -U, only a literal % needs care: write it as %25.
axios and HTTP_PROXY
axios reads HTTP_PROXY itself. With the password encoded, it sent the encoded text as the password and got 407; unencoded, the variable isn’t a valid URL. Use the proxy option with auth: { username, password }, which takes the password as is. More on axios and proxies.
Python’s quote() keeps the slash
urllib.parse.quote() leaves / alone unless you pass safe="". For the password se/cret@7, requests failed with Failed to parse: 'bob:se' is not a valid host or port; with safe="" it got 200.
A setup that works
Both ran unchanged against our gateway and, with the host swapped, against the Squid with p@ss:w0rd#1%2F and se/cret@7. PROXY_USER and PROXY_PASS come from the environment.
import os
from urllib.parse import quote
import requests # pip install requests
user = quote(f"{os.environ['PROXY_USER']}-country-de", safe="")
password = quote(os.environ["PROXY_PASS"], safe="") # safe="": also encode "/", which quote() keeps by default
proxy = f"http://{user}:{password}@via.proxies.vision:9000"
r = requests.get("https://ipinfo.io/json", proxies={"http": proxy, "https": proxy}, timeout=30)
print(r.status_code, r.json()["country"])import { ProxyAgent, fetch } from "undici"; // npm i undici
const user = encodeURIComponent(`${process.env.PROXY_USER}-country-de`);
const pass = encodeURIComponent(process.env.PROXY_PASS);
const dispatcher = new ProxyAgent(`http://${user}:${pass}@via.proxies.vision:9000`);
const res = await fetch("https://ipinfo.io/json", { dispatcher });
console.log(res.status, (await res.json()).country);200 DEOn the command line
curl -x http://via.proxies.vision:9000 -U "$PROXY_USER-country-de:$PROXY_PASS" https://ipinfo.io/json keeps the login out of the URL. It is safe as long as the password has no %; if it does, write each one as %25. If curl still answers 407.
For a password of only letters and digits the encoding changes nothing, so there is no reason to leave it out: the same code keeps working when a password with symbols comes along. Targeting goes into the username before encoding: USER-country-de-session-job1-lifetime-10 on port 10000 keeps one IP for ten minutes.
How do I put a password with @ into a proxy URL?
Why does curl say 'Port number was not a decimal number between 0 and 65535'?
Why does quote() still fail on a password with a slash?
Why does axios get a 407 with an encoded password in HTTP_PROXY?
Keep reading
git: Proxy CONNECT aborted and 407 From Proxy (Fix)
git clone through a proxy fails with Proxy CONNECT aborted or 407 after CONNECT. Why it happens, the one git config line that fixes it, and SSH remotes.
4 min read · #errors#authentication
wget Proxy Authentication: https_proxy, 407 and an Ignored HTTPS_PROXY
wget through a proxy with a login: which variables it reads (not HTTPS_PROXY), --proxy-user, what 'Proxy tunneling failed' means, and BusyBox wget.
5 min read · #command-line#authentication
aiohttp Proxy Errors: ClientHttpProxyError 407, SOCKS5 and trust_env
aiohttp's ClientHttpProxyError explained, why its message leaks your proxy password, socks5:// without aiohttp-socks, and rotation.
6 min read · #python#errors
Residential proxies, pay per GB
HTTP and SOCKS5. Country, city and sticky sessions in the username. No subscription.