URL Encoding: The Developer's Swiss Army Knife You're Probably Using Wrong

Published: (May 9, 2026 at 10:51 AM EDT)
2 min read
Source: Dev.to

Source: Dev.to

TL;DR

encodeURI() = “I’m encoding a complete URL” → keeps structural chars like / ? & #

encodeURIComponent() = “I’m encoding ONE piece of data” → encodes everything except A-Z a-z 0-9 - _ . ! ~ * ' ( )

When Things Go Wrong

Last week I spent an hour debugging a 500 error from a payment gateway. The culprit? I used encodeURI() on a query parameter that contained a # character. The browser interpreted everything after # as a fragment identifier and never sent it to the server. encodeURIComponent() would have caught it.

The 3 Rules I Now Live By

  • Query string valuesALWAYS encodeURIComponent(). No exceptions.
  • Full URLsencodeURI(), but only if you’re sure no component contains special chars.
  • When in doubtencodeURIComponent(). Worst case: you encode too much. Best case: you avoid a bug.

A Free Tool for When You’re Lazy

Sometimes I just want to paste a gnarly URL and see what happens — especially when it’s 200 characters long and I don’t want to open DevTools. I built a free URL encoder/decoder that runs entirely in the browser. No uploads, no ads, no signup. Just paste and encode.

0 views
Back to Blog

Related posts

Read more »