Home βΊ URL Encoder / Decoder & Query String Parser
URL Encoder / Decoder & Query String Parser
Query string parser
What URL encoding does
URLs may only contain a limited set of ASCII characters. Everything else β spaces, accented letters, emoji, and characters that have a structural meaning such as &, =, ? and # β must be percent-encoded: the character is converted to UTF-8 bytes and each byte is written as % followed by two hex digits. For example cafΓ© becomes caf%C3%A9 and a space becomes %20.
Examples
| Input | encodeURIComponent | encodeURI |
|---|---|---|
a b&c=d | a%20b%26c%3Dd | a%20b&c=d |
https://x.io/?q=1 | https%3A%2F%2Fx.io%2F%3Fq%3D1 | https://x.io/?q=1 |
100% π | 100%25%20%F0%9F%98%80 | 100%25%20%F0%9F%98%80 |
Gotchas
- Double encoding. If you see
%2520in a URL, a value was encoded twice (%β%25). Decode once and check where the second encoding happened, often a framework re-encoding a value you already encoded. - Repeated keys.
?tag=a&tag=bis valid and common for arrays. Some frameworks keep only the last value, PHP needstag[]=. The parser above lists every occurrence. - The fragment (
#β¦) is never sent to the server. OAuth implicit flows put tokens there for exactly that reason. - Building URLs in code: prefer
new URL()andurl.searchParams.set(k, v)in JavaScript,urllib.parse.urlencodein Python, orurl.Valuesin Go instead of concatenating strings.
Frequently asked questions
What is the difference between encodeURI and encodeURIComponent?
encodeURI is meant for a full URL and leaves characters with special meaning in URLs (: / ? # & = +) untouched. encodeURIComponent is meant for a single value such as a query parameter and encodes those characters too. Use encodeURIComponent for parameter values; using encodeURI there breaks values that contain & or =.
Should a space be encoded as %20 or +?
In the path and in general percent-encoding, a space is %20. In HTML form data (application/x-www-form-urlencoded), which is also what URLSearchParams produces, a space is +. When decoding query strings that came from forms, enable 'Treat + as space'.
Why do I get 'URI malformed' when decoding?
decodeURIComponent throws when it meets a % that is not followed by two hex digits, or a sequence that is not valid UTF-8 (for example %E9 on its own, which is Latin-1 Γ©). Encode a literal percent sign as %25.
Which characters are never encoded?
encodeURIComponent leaves A-Z a-z 0-9 and - _ . ! ~ * ' ( ) unchanged. RFC 3986 considers only - _ . ~ as 'unreserved', so some strict APIs (for example OAuth 1.0 signatures) require encoding ! * ' ( ) as well; the 'RFC 3986 strict' option does that.