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"

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