URL encode and decode

Percent-encode text for URLs and query strings, or decode it back.

This tool runs in your browser. We never send what you type or paste to a server.

Component vs full URL

  • Component (encodeURIComponent) encodes everything except letters, digits and - _ . ! ~ * ' ( ). Use it for a single query value or path segment.
  • Full URL (encodeURI) leaves characters that structure a URL, like : / ? # & =, alone. Use it for a whole address that contains spaces or non-English characters.

Spaces: %20 or +?

In URLs, a space is %20. In HTML form data (application/x-www-form-urlencoded), a space is sent as +. When decoding, tick Treat + as space for form-style data.

Example

café & croissants?  →  caf%C3%A9%20%26%20croissants%3F

In Component mode the & and ? are encoded too, so the text can sit safely inside a single query value such as ?q=… without breaking the rest of the URL.

How percent-encoding picks bytes

A URL may only contain a limited set of ASCII characters. RFC 3986 calls A–Z a–z 0–9 - . _ ~ “unreserved”: they never need encoding. Everything else is first turned into UTF-8 bytes, and each byte is written as % followed by two hex digits (the RFC recommends upper case). That is why one character can become three, six, nine or twelve characters:

for (const ch of ['é', '€', '😀']) console.log(ch.codePointAt(0).toString(16), encodeURIComponent(ch));
// → e9 %C3%A9
// → 20ac %E2%82%AC
// → 1f600 %F0%9F%98%80

Reserved characters such as / ? # & = : have a job in the URL's structure. Leave them as they are when they do that job, and encode them when they are part of the data, such as an & inside a search term. That is the whole difference between the two modes of this tool: Component encodes them, Full URL does not.

Encoding a string in JavaScript

For a single value, encodeURIComponent is right almost every time. To build a query string, let URL and URLSearchParams do it: they apply form encoding, so spaces come out as +, and they decode + back to a space when reading.

const value = 'fish & chips/50% off';
console.log(encodeURIComponent(value));      // → fish%20%26%20chips%2F50%25%20off
const url = new URL('https://example.com/search');
url.searchParams.set('q', value);            // form encoding: space becomes +
console.log(url.href);                       // → https://example.com/search?q=fish+%26+chips%2F50%25+off
console.log(decodeURIComponent('fish%20%26%20chips'));         // → fish & chips
console.log(new URLSearchParams('q=fish+%26+chips').get('q'));  // → fish & chips

encodeURIComponent follows the older RFC 2396 and leaves ! ' ( ) * unescaped. Most servers do not care, but request-signing schemes that follow RFC 3986 strictly, such as OAuth 1.0 signatures and AWS Signature Version 4, need them escaped:

// encodeURIComponent leaves ! ' ( ) * alone; RFC 3986 reserves them, so some signing schemes (OAuth 1.0, AWS) need them escaped.
const rfc3986 = (s) => encodeURIComponent(s).replace(/[!'()*]/g, (c) => `%${c.charCodeAt(0).toString(16).toUpperCase()}`);
console.log(rfc3986("it's (nearly) done!"));     // → it%27s%20%28nearly%29%20done%21

Python, the shell and PowerShell

Python's urllib.parse.quote treats / as safe by default, which is right for a path but wrong for a query value. Pass safe='', or use urlencode for whole query strings:

from urllib.parse import quote, quote_plus, unquote, unquote_plus, urlencode
value = 'fish & chips/50% off'
print(quote(value))                  # → fish%20%26%20chips/50%25%20off   / is left alone by default
print(quote(value, safe=''))         # → fish%20%26%20chips%2F50%25%20off
print(quote_plus(value))             # → fish+%26+chips%2F50%25+off
print(urlencode({'q': value, 'page': 2}))   # → q=fish+%26+chips%2F50%25+off&page=2
print(unquote_plus('fish+%26+chips'))       # → fish & chips

Decoding assumes UTF-8 and replaces bytes that are not valid UTF-8, so old Latin-1 links need the encoding named (this tool guesses Windows-1252 for those):

print(unquote('caf%C3%A9'))                      # → café
print(unquote('caf%E9'))                         # → caf�   not UTF-8: replaced
print(unquote('caf%E9', encoding='latin-1'))     # → café

Bash has no built-in URL encoder. This function escapes every byte outside the unreserved set; LC_ALL=C makes it walk bytes rather than characters, so UTF-8 comes out right:

urlencode() {   # keeps only A-Z a-z 0-9 - . _ ~, escapes every other byte
  local LC_ALL=C s="$1" out='' c i
  for (( i = 0; i < ${#s}; i++ )); do
    c=${s:i:1}
    case $c in
      [A-Za-z0-9.~_-]) out+=$c ;;
      *) out+=$(printf '%%%02X' "'$c") ;;
    esac
  done
  printf '%s\n' "$out"
}
urldecode() { printf '%b\n' "$(printf '%s' "${1//+/ }" | sed 's/%/\\x/g')"; }
urlencode 'fish & chips/50% off'     # → fish%20%26%20chips%2F50%25%20off
urlencode 'café'                     # → caf%C3%A9
urldecode 'fish+%26+chips'           # → fish & chips

If you are calling an API with curl, curl -G --data-urlencode 'q=fish & chips' URL encodes the value and appends it as a query string for you. In PowerShell and .NET, EscapeDataString gives %20 and WebUtility.UrlEncode gives form-style +:

[Uri]::EscapeDataString('fish & chips/50% off')       # → fish%20%26%20chips%2F50%25%20off
[Net.WebUtility]::UrlEncode('fish & chips/50% off')   # → fish+%26+chips%2F50%25+off
[Uri]::UnescapeDataString('fish%20%26%20chips')       # → fish & chips
[Net.WebUtility]::UrlDecode('fish+%26+chips')         # → fish & chips

Mistakes that break URLs

  • Using encodeURI on a value. It leaves &, =, ? and # alone, so a value containing them splits into extra parameters or loses everything after #.
  • Decoding + with decodeURIComponent. It only understands %20. Form data and query strings built by HTML forms use +; read them with URLSearchParams or unquote_plus.
  • Encoding twice. Encoding an encoded string turns every % into %25. Encode each value once, at the moment you put it into the URL.
  • A bare %. Text like 100% is not valid percent-encoding, and decodeURIComponent throws URIError. Encode it as %25.
console.log(encodeURI('https://example.com/?item=fish & chips'));   // → https://example.com/?item=fish%20&%20chips   & splits the value
console.log(decodeURIComponent('fish+chips'));   // → fish+chips   + is only a space in form data
console.log(encodeURIComponent('%20'));          // → %2520   encoded twice
try { decodeURIComponent('100%'); } catch (e) { console.log(e.name); }   // → URIError

Questions

What does %20 mean?

It's a percent-encoded space: 20 is the hexadecimal code for the space character.

Why is my URL encoded twice?

Encoding an already-encoded string turns % into %25, so %20 becomes %2520. Decode once to fix it.

Why does %E9 fail to decode?

Modern URLs use UTF-8, where é is %C3%A9. A lone %E9 comes from an older Latin-1 or Windows-1252 page; the tool says so and shows its best guess at the original text.

How do I URL-encode a string in JavaScript?

Use encodeURIComponent(value) for a single query value or path segment. To build a whole query string, set the values on new URL(...).searchParams or a URLSearchParams object and let it encode them. Use encodeURI only for a complete URL that has spaces or non-ASCII characters in it.

Why does Python's quote() not encode slashes?

Its safe parameter defaults to '/' because it was designed for URL paths. Call quote(value, safe='') to encode slashes too, or use quote_plus or urlencode for query strings.

Is %2F the same as / in a URL?

Not always. A / separates path segments, while %2F is a slash inside a segment's data. Many servers decode them the same way, but some refuse %2F in paths or route it differently, so only encode a slash when it really is data.

Should percent-encoding use upper or lower case hex?

Both mean the same, and decoders accept either. RFC 3986 recommends upper case, and that is what JavaScript, Python and this tool produce, which matters when you compare or sign URLs as strings.