%20 Encoding

URL Encoding & Decoding Code Examples

Copy-paste URL encoding and decoding snippets for JavaScript, Python, PHP, Go, Java, Rust, and more. Handle special characters correctly.

URL Encode in JavaScript

javascript

JavaScript provides encodeURIComponent() for query params and encodeURI() for full URLs.

// Encode a query parameter value
const param = encodeURIComponent('hello world & more');
console.log(param); // "hello%20world%20%26%20more"

// Encode a full URL (preserves :, /, ?, #, etc.)
const url = encodeURI('https://example.com/path?q=hello world');
console.log(url); // "https://example.com/path?q=hello%20world"

// Build a query string safely
const params = new URLSearchParams({
  query: 'hello world',
  page: '1',
  filter: 'name=test&value=42'
});
console.log(params.toString());

URL Decode in JavaScript

javascript

Decode URL-encoded strings back to readable text.

// Decode a component
const decoded = decodeURIComponent('hello%20world%20%26%20more');
console.log(decoded); // "hello world & more"

// Decode a full URL
const url = decodeURI('https://example.com/path?q=hello%20world');
console.log(url);

// Parse query string parameters
const params = new URLSearchParams('?query=hello+world&page=1');
console.log(params.get('query')); // "hello world"

URL Encode in Python

python

Use urllib.parse for URL encoding in Python 3.

from urllib.parse import quote, urlencode

# Encode a single string
encoded = quote('hello world & more')
print(encoded)  # "hello%20world%20%26%20more"

# Encode query parameters
params = urlencode({
    'query': 'hello world',
    'page': 1,
    'filter': 'name=test&value=42'
})
print(params)

# quote_plus uses + for spaces (form encoding)
from urllib.parse import quote_plus
print(quote_plus('hello world'))  # "hello+world"

URL Decode in Python

python

Decode percent-encoded URLs in Python.

from urllib.parse import unquote, parse_qs, urlparse

# Decode a string
decoded = unquote('hello%20world%20%26%20more')
print(decoded)  # "hello world & more"

# Parse a full URL with query params
url = 'https://example.com/search?q=hello%20world&page=2'
parsed = urlparse(url)
params = parse_qs(parsed.query)
print(params)  # {'q': ['hello world'], 'page': ['2']}

URL Encode in PHP

php

PHP offers urlencode() and rawurlencode() for different encoding standards.

<?php
// urlencode: spaces become +
echo urlencode('hello world & more');
// "hello+world+%26+more"

// rawurlencode: spaces become %20 (RFC 3986)
echo rawurlencode('hello world & more');
// "hello%20world%20%26%20more"

// Build a query string
$params = http_build_query([
    'query' => 'hello world',
    'page' => 1,
]);
echo $params; // "query=hello+world&page=1"

URL Decode in PHP

php

Decode URL-encoded strings in PHP.

<?php
echo urldecode('hello+world+%26+more');
// "hello world & more"

echo rawurldecode('hello%20world%20%26%20more');
// "hello world & more"

// Parse a query string into an array
parse_str('query=hello+world&page=1', $params);
print_r($params);
// Array ( [query] => hello world [page] => 1 )

URL Encode in Go

go

Use net/url package for URL encoding in Go.

package main

import (
    "fmt"
    "net/url"
)

func main() {
    // Encode a single value
    encoded := url.QueryEscape("hello world & more")
    fmt.Println(encoded) // "hello+world+%26+more"

    // Path encoding (spaces -> %20)
    pathEncoded := url.PathEscape("hello world & more")
    fmt.Println(pathEncoded)

    // Build query params safely
    params := url.Values{}
    params.Set("query", "hello world")
    params.Set("page", "1")
    fmt.Println(params.Encode())
}

URL Decode in Go

go

Decode URL-encoded strings and parse query parameters in Go.

package main

import (
    "fmt"
    "net/url"
)

func main() {
    decoded, err := url.QueryUnescape("hello+world+%26+more")
    if err != nil {
        panic(err)
    }
    fmt.Println(decoded) // "hello world & more"

    // Parse a full URL
    u, _ := url.Parse("https://example.com/search?q=hello+world&page=2")
    fmt.Println(u.Query().Get("q")) // "hello world"
}

URL Encode in Java

java

Use URLEncoder class for encoding query parameters.

import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;

// Encode a query parameter
String encoded = URLEncoder.encode(
    "hello world & more", StandardCharsets.UTF_8
);
System.out.println(encoded); // "hello+world+%26+more"

// For path segments, replace + with %20
String pathEncoded = encoded.replace("+", "%20");
System.out.println(pathEncoded);

URL Decode in Java

java

Decode URL-encoded strings in Java.

import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;

String decoded = URLDecoder.decode(
    "hello+world+%26+more", StandardCharsets.UTF_8
);
System.out.println(decoded); // "hello world & more"

URL Encode in Rust

rust

Use the urlencoding crate or percent-encoding crate.

// Cargo.toml: urlencoding = "2"
use urlencoding::{encode, decode};

fn main() {
    // Encode
    let encoded = encode("hello world & more");
    println!("{}", encoded); // "hello%20world%20%26%20more"

    // Decode
    let decoded = decode("hello%20world%20%26%20more").unwrap();
    println!("{}", decoded); // "hello world & more"
}

URL Encode in C#

csharp

Use Uri.EscapeDataString or HttpUtility.UrlEncode in .NET.

using System;
using System.Web;

// RFC 3986 compliant (recommended)
string encoded = Uri.EscapeDataString("hello world & more");
Console.WriteLine(encoded); // "hello%20world%20%26%20more"

// Form encoding (+ for spaces)
string formEncoded = HttpUtility.UrlEncode("hello world & more");
Console.WriteLine(formEncoded); // "hello+world+%26+more"

// Decode
string decoded = Uri.UnescapeDataString("hello%20world");
Console.WriteLine(decoded); // "hello world"

URL Encode in Ruby

ruby

Use CGI.escape or URI.encode_www_form_component.

require 'cgi'
require 'uri'

# Form encoding (spaces -> +)
puts CGI.escape('hello world & more')
# "hello+world+%26+more"

# Decode
puts CGI.unescape('hello+world+%26+more')
# "hello world & more"

# Build query string
puts URI.encode_www_form(query: 'hello world', page: 1)
# "query=hello+world&page=1"

URL Encode in Bash / curl

bash

URL encode strings using curl or Python one-liners.

# Using curl's --data-urlencode
curl -G --data-urlencode "q=hello world & more" https://example.com/search

# Using Python one-liner
echo "hello world & more" | python3 -c \
  "import sys, urllib.parse; print(urllib.parse.quote(sys.stdin.read().strip()))"
# Output: hello%20world%20%26%20more

URL Encoding Special Characters Reference

text

Quick reference for common URL-encoded characters.

Character  Encoded    Notes
---------  -------    -----
(space)    %20        or + in form data
!          %21
#          %23        Fragment identifier
$          %24
%          %25        Escape character itself
&          %26        Parameter separator
+          %2B        Space in form encoding
/          %2F        Path separator
=          %3D        Key-value separator
?          %3F        Query string start
@          %40

Need to work with url encode/decode right now?

Use our free online url encoder — no signup, no install, works in your browser.

Open URL Encoder →

More Code Snippets