URL Encoder / Decoder

Encode or decode URLs — query string, path segment, or form-encoded. Code snippets for Go, Python, JS, PHP, Java.

encodeURIComponent — for query string values

Code Snippet

What is URL Encoding?

URL encoding (also called percent-encoding) converts characters that are not safe to use in a URL into a special format: a percent sign followed by two hexadecimal digits. For example, a space becomes %20, and an ampersand becomes %26.

URLs can only contain a limited set of safe characters: letters (A–Z, a–z), digits (0–9), and a few special characters like - _ . ~. All other characters — including spaces, Unicode characters, and structural URL characters like ? & = / : — must be encoded when used as values inside a URL.

There are three main encoding contexts: query string encoding (spaces → %20), form encoding (spaces → + per HTML form spec), and path encoding. Each has slightly different rules — the tool handles all three.

URL Encoding Types

TypeSpacesSpecial charsUse case
Query (encodeURIComponent)%20% percent-encodedQuery param values: ?q=hello%20world
Path encoding%20% percent-encoded (preserves /)URL path segments: /users/john%20doe
Form (application/x-www-form-urlencoded)+% percent-encodedPOST body, HTML forms, application/x-www-form-urlencoded

Go URL Encoding

go
import "net/url"

// Query value encoding (spaces → %20)
raw := "Hello World! / test"
encoded := url.QueryEscape(raw)
// => "Hello+World%21+%2F+test"   (QueryEscape uses + for spaces)

// PathEscape (spaces → %20, safe for path segments)
pathEncoded := url.PathEscape(raw)
// => "Hello%20World%21%20%2F%20test"

// Build a URL with properly encoded query params
base := "https://api.example.com/search"
params := url.Values{}
params.Set("q", "hello world")
params.Set("lang", "go+python")
fullURL := base + "?" + params.Encode()
// => "https://api.example.com/search?lang=go%2Bpython&q=hello+world"

// Parse and decode a URL
u, err := url.Parse("https://example.com/path?name=John%20Doe")
if err != nil { panic(err) }
fmt.Println(u.Query().Get("name")) // => "John Doe"

Python URL Encoding

python
from urllib.parse import quote, quote_plus, urlencode, parse_qs

# Query value encoding (spaces -> %20)
quote("Hello World! / test")        # 'Hello%20World%21%20/%20test'

# Form encoding (spaces -> +), matches application/x-www-form-urlencoded
quote_plus("Hello World!")          # 'Hello+World%21'

# Build a query string from a dict
params = {"q": "hello world", "lang": "py"}
urlencode(params)                   # 'q=hello+world&lang=py'

# Parse a query string back to a dict
parse_qs("name=John%20Doe&age=30")  # {'name': ['John Doe'], 'age': ['30']}

JavaScript URL Encoding

javascript
// Encode a single value for use in a query string
encodeURIComponent("hello world & more"); // "hello%20world%20%26%20more"

// Encode a full URL, preserving structural characters (: / ? & = #)
encodeURI("https://example.com/search?q=hello world");
// "https://example.com/search?q=hello%20world"

// Build a query string from an object
const params = new URLSearchParams({ q: "hello world", lang: "js" });
params.toString(); // "q=hello+world&lang=js"

// Decode back
decodeURIComponent("hello%20world"); // "hello world"

Where URL Encoding Matters

  • Building API query strings by handEncode a search term, filter value or free-text parameter before appending it to a URL so special characters don't break the request.
  • Debugging a broken linkDecode a percent-encoded URL from logs or a browser address bar to see the actual value a user submitted.
  • Passing data through a redirectEncode a return URL or state value that itself contains query parameters, so nesting doesn't corrupt the outer URL.
  • Preparing a form-encoded POST bodyConvert key-value pairs into application/x-www-form-urlencoded format when an API doesn't accept JSON.

Frequently Asked Questions

encodeURI encodes a full URL and preserves characters like : / ? & = # that are part of URL structure. encodeURIComponent encodes a single URL component (like a query value) and encodes those structural characters too. Always use encodeURIComponent for query param values.

url.QueryEscape follows the application/x-www-form-urlencoded specification where spaces are represented as + for compactness. Use url.PathEscape if you need %20 for path segments. Both are valid in their respective contexts.

Characters outside the unreserved set (A-Z a-z 0-9 - _ . ~) must be percent-encoded. Space becomes %20 or +. & = ? # / : @ are structural and must be encoded in values (but not in URL structure).

Use url.Values: vals := url.Values{}; vals.Set("key", "value"); qs := vals.Encode(). This properly encodes all values and sorts keys alphabetically for reproducible output.

Related Tools