Skip to content
toolbelt
Back to Guides

The Double-Encoding Bug You Will Hit Eventually

By ·

Updated

A space becomes %20, then %2520, and suddenly your search returns nothing. Here is why, and how to stop it.

What is this tool?

URL encoding replaces characters that are illegal or ambiguous in a URL with percent-escapes. Correct encoding is the difference between a working query and a mysterious empty result.

How it works

There are two functions and using the wrong one is the usual mistake. encodeURIComponent encodes everything meaningfully, including slashes and question marks, so it suits a single value. encodeURI leaves URL structure intact, so it suits a whole URL. I give you both.

Example

Value: a b&c

encodeURIComponent → a%20b%26c
encodeURI          → a%20b&c

Paste the second into a query parameter and the & splits your value in two.

When to use it

Building a URL with a search term, a file path, or any user input. Debugging why a link with a plus sign in it behaves oddly. Reading logs where everything is percent-escaped.

Optimization Tips

Encode once, at the point where you build the URL. If you encode in the form handler and again in the request builder, you get %2520. When you see a double-encoded value, remove the earlier encoding rather than adding a decode.

Frequently Asked Questions

When do I use which?
encodeURIComponent for a value going inside a URL. encodeURI for a complete URL you are passing elsewhere.
Why does + become a space sometimes?
In query strings, + historically meant space. In paths it means a literal plus. This is a real inconsistency in the standard.
What is %2F?
An encoded forward slash. It matters when you want a slash inside a path segment rather than as a separator.
Does decoding ever fail?
Yes, on malformed sequences like %ZZ. The tool reports it rather than guessing.

Ready to try it out?

Put this knowledge into practice with our free tool.

Go to Tool

Related tools