%url-decoder.net

How to Put a Password With Special Characters in a URL

Updated November 18, 2026 · 5 min read

Short answer

Percent-encode the username and password on their own, then build the URL. Characters such as @ : / ? # [ ] $ in a password break the connection string otherwise, because the parser reads them as URL delimiters. In JavaScript use encodeURIComponent; in Python use quote(password, safe='').

Database drivers, message brokers and many APIs take a URL that includes credentials: scheme://user:password@host/path. When the password contains a character that also has a job in URL syntax, the parser cuts the URL in the wrong place, and the error you see is usually a confusing authentication or host-not-found failure.

Why special characters break the URL

The credentials end at the last @ before the host, and : separates the user from the password. A password like p@ss:w0rd/#?$ contains an @, a :, a /, a # and a ?. Each one can end the credentials, start the path, the query or the fragment early.

Encoded, the same password is unambiguous:

postgresql://app_user:p%40ss%3Aw0rd%2F%23%3F%24@db.example.com:5432/app

What the drivers say

  • MongoDB documents that if the username or password includes $ : / ? # [ ] @, those characters must be converted using percent-encoding. Its own example writes a password containing @ as D1fficultP%40ssw0rd (MongoDB connection string formats).
  • PostgreSQL (libpq) says a connection URI needs percent-encoding if it includes symbols with special meaning in any of its parts (libpq connection strings).
  • For any other driver, check its documentation; the RFC 3986 rule below is always safe.

How to encode it, by language

Outputs below were produced by running each function, for the password p@ss:w0rd/#?$.

CallOutputRuntime
encodeURIComponent(password)p%40ss%3Aw0rd%2F%23%3F%24Node.js 18.20.7 (V8 10.2.154.26-node.39)
urllib.parse.quote(password, safe='')p%40ss%3Aw0rd%2F%23%3F%24Python 3.12.6
urllib.parse.quote(password) ← keeps /p%40ss%3Aw0rd/%23%3F%24Python 3.12.6
Uri.EscapeDataString(password)p%40ss%3Aw0rd%2F%23%3F%24.NET 8.0.6

Note the third row: Python's quote keeps / by default, which leaves the password broken. Always pass safe=''. Form-style encoders that write spaces as +, such as quote_plus or URLSearchParams, are also the wrong choice for credentials: a URL parser does not turn + back into a space. Compare every function.

Checklist

  1. Encode the username and the password separately, never the whole URL.
  2. Encode once. If the password already contains % sequences from an earlier step, do not encode it again.
  3. Check the result in the URL Parser: the username and password should appear in their own fields and the host should be correct.
  4. Keep the URL out of logs and source control: encoding hides nothing.

Questions

Which characters in a password must be encoded in a URL?

At least : / ? # [ ] @, the URL delimiters; MongoDB's documentation also lists $. Encoding every character other than letters, digits and - . _ ~ is always safe, and it is what encodeURIComponent and Uri.EscapeDataString do.

Should I encode the whole connection string?

No. Encode only the username and password. The : between them and the @ before the host must stay as real delimiters, or the URL can no longer be parsed.

Why does my database say authentication failed after encoding?

The password may have been encoded twice, so the server received %2540 instead of @. Encode once, at the point where the URL is built, and do not encode values that are already encoded.

Is a password in a URL safe?

Encoding is not protection: anyone who sees the URL can decode the password. Keep connection strings out of logs and source control, and prefer separate configuration fields or a secrets manager where the driver supports them.

See also

Try everything in this guide hands-on, free and entirely in your browser.

Encode a password in the URL Encoder

Keep reading