DevRivet

Blog ·

URL Encode Space: Why %20 or + Shows Up in URLs

A space becomes %20 or + in a URL depending on where it appears and which encoder made it. Here is how each works, with JavaScript and Python examples.

URL Encoder / Decoder

Encode and decode URL/query-string components.

Try it free →

A space is encoded as %20 in most URL contexts. The + sign means a space only in form-encoded query strings (application/x-www-form-urlencoded), which is what HTML forms and URLSearchParams produce. If you are encoding a path or using encodeURIComponent, use %20. If you are reading or writing a query string from a form or a library that uses form encoding, + may mean a space.

If you want to see the output for a specific string, the URL Encoder / Decoder shows the percent-encoded result and decodes it back. The rest of this post explains why the two forms exist and where they cause bugs.

Why a space can't appear in a URL

URLs are defined to contain only a limited set of ASCII characters. The space character (0x20) is not one of them, so it has to be written in some other form. Percent-encoding, defined in RFC 3986, does this by writing a byte as a percent sign followed by two hexadecimal digits. The space byte is 0x20, so it becomes %20.

Any byte can be percent-encoded, but encoders only do it for characters that are unsafe in the part of the URL they are writing. Letters, digits and a few symbols like - _ . ~ stay as they are.

Where + comes from

The + sign has a second meaning that comes from HTML forms. When a browser submits a form using the default GET or POST encoding, it serializes the fields as key=value pairs joined by &. Spaces in values are written as +, and literal plus signs are written as %2B. This convention is defined in the WHATWG URL Standard and in the older HTML spec for application/x-www-form-urlencoded.

So the same space can be written two ways, and both are valid in the right context:

  • %20: the general percent-encoding of a space, valid in paths, query strings, fragments and anywhere else.
  • +: a space only inside a form-encoded query string or form body. Outside that context, + is a literal plus sign.

Which encoder produces which

The output depends on the function you call, not on the URL itself. This table covers the common ones:

  • JavaScript encodeURIComponent("a b") gives a%20b. It never produces +.
  • JavaScript encodeURI("https://example.com/a b") gives https://example.com/a%20b. It does not encode reserved characters like & or =, so it is not safe for values.
  • JavaScript new URLSearchParams({ q: "a b" }).toString() gives q=a+b. It uses form encoding.
  • Python urllib.parse.quote("a b") gives a%20b. It is the path-style encoder.
  • Python urllib.parse.quote_plus("a b") gives a+b. It is form-style.
  • Python urllib.parse.urlencode({"q": "a b"}) gives q=a+b, because it uses quote_plus by default.

Examples in JavaScript

encodeURIComponent("hello world");
// "hello%20world"

new URLSearchParams({ q: "hello world" }).toString();
// "q=hello+world"

new URLSearchParams({ q: "a+b" }).toString();
// "q=a%2Bb"

encodeURIComponent("a+b");
// "a%2Bb"

Notice that encodeURIComponent also encodes the plus sign, so a literal + becomes %2B. That matters because a bare + in a query string will be read back as a space by most servers.

Examples in Python

from urllib.parse import quote, quote_plus, urlencode

quote("hello world")       # 'hello%20world'
quote_plus("hello world")  # 'hello+world'
urlencode({"q": "a b"})    # 'q=a+b'
urlencode({"q": "a+b"})    # 'q=a%2Bb'

Decoding: where + is read as a space

Decoders follow the same split. Most bugs happen when a decoder for one context is used on data from the other.

  • JavaScript decodeURIComponent("a+b") returns "a+b". It does not turn + into a space.
  • JavaScript decodeURIComponent("a%20b") returns "a b".
  • JavaScript new URLSearchParams("q=a+b").get("q") returns "a b".
  • Python unquote("a+b") returns "a+b".
  • Python unquote_plus("a+b") returns "a b".

The rule to remember: decodeURIComponent and unquote handle %20 but leave + alone. URLSearchParams and unquote_plus treat + as a space. If your decoded value has stray plus signs where you expected spaces, you are probably using a percent-decoder on form-encoded data.

Common pitfalls

  • Path segments use %20 only. Writing /files/my+file.txt means a file named "my+file.txt" to most servers, not "my file.txt".
  • Double encoding. If you encode a value that is already encoded, %20 becomes %2520. The percent sign itself is encoded as %25. Check whether a string is already encoded before encoding it again.
  • Mixing encoders in one string. Using URLSearchParams for one parameter and encodeURIComponent for another gives inconsistent output that some servers read differently.
  • Plus in tokens or base64. A literal + in a value read from a query string may have been a space in the original or a plus that was never encoded. Always encode + as %2B in values you build yourself.
  • Server frameworks differ. Most frameworks decode + as a space in query strings, but some do not decode form bodies or path segments the same way. Test with a real request when behavior matters.

Which one should you use?

Use the encoder that matches the place in the URL where the value goes:

  • Path segments, fragments, and general URL building: encodeURIComponent in JavaScript or quote in Python, which produce %20.
  • Query string values where you want form-style output: URLSearchParams in JavaScript or urlencode in Python, which produce +.
  • Decoding a query string from a request or a form: use a form-aware parser (URLSearchParams or parse_qs) so + becomes a space.
  • Decoding a single component you already extracted: use decodeURIComponent or unquote, which keep + as a plus sign.

For a deeper look at the JavaScript functions, see encodeURIComponent vs encodeURI in JavaScript: Which to Use. The Base64 Encoder / Decoder is a separate tool, but note that Base64URL uses - and _ instead of + and /, which is a related source of confusion when tokens end up in URLs. The Base64 vs Base64URL post covers that.

DevRivet's URL Encoder runs entirely in your browser, so you can paste query strings that contain tokens or other sensitive values without sending them to a server.

FAQ

Is %20 always correct for a space in a URL?

Yes, %20 is the correct percent-encoding for a space in any part of a URL. It is the safe choice when you are unsure which context you are in, because no parser treats %20 as anything other than a space.

Why does my server show + instead of a space?

The value was probably decoded with a percent-decoder instead of a form-aware parser, or the sender used form encoding and the receiver is not decoding + as a space. Check which function is reading the value and whether the source used URLSearchParams or encodeURIComponent.

How do I write a literal plus sign in a query string?

Encode it as %2B. For example, the value a+b should appear as a%2Bb in the query string. This works with both form-style and percent-style decoders.

Does the + sign mean a space in a URL path?

No. Outside form-encoded query strings and bodies, + is a literal plus sign. Use %20 for spaces in paths.

URL Encoder / Decoder

Encode and decode URL/query-string components.

Try it free →