// URL encode a string
const encoded = encodeURIComponent("hello world & foo=bar");
// "hello%20world%20%26%20foo%3Dbar"
// URL decode a string
const decoded = decodeURIComponent("hello%20world%20%26%20foo%3Dbar");
// "hello world & foo=bar"
// Encode special characters
encodeURIComponent("price=100€&tax=20%");
// "price%3D100%E2%82%AC%26tax%3D20%25"
// Build query parameters from an object
const params = { q: "hello world", page: "1", lang: "en" };
const query = Object.entries(params)
.map(([k, v]) => `${encodeURIComponent(k)}=${encodeURIComponent(v)}`)
.join("&");
// "q=hello%20world&page=1&lang=en"
// Parse query parameters from a URL
const url = new URL("https://example.com?q=hello%20world&page=1");
const q = url.searchParams.get("q"); // "hello world"
// Using URLSearchParams for encoding
const search = new URLSearchParams({ q: "café", sort: "name" });
search.toString(); // "q=caf%C3%A9&sort=name"
// Encode a full URI (preserves :, /, ?, #, etc.)
const fullUri = encodeURI("https://example.com/path?q=hello world");
// "https://example.com/path?q=hello%20world"
// URL encode a string
const encoded = encodeURIComponent("hello world & foo=bar");
// "hello%20world%20%26%20foo%3Dbar"
// URL decode a string
const decoded = decodeURIComponent("hello%20world%20%26%20foo%3Dbar");
// "hello world & foo=bar"
// Encode special characters
encodeURIComponent("price=100€&tax=20%");
// "price%3D100%E2%82%AC%26tax%3D20%25"
// Build query parameters from an object
const params = { q: "hello world", page: "1", lang: "en" };
const query = Object.entries(params)
.map(([k, v]) => `${encodeURIComponent(k)}=${encodeURIComponent(v)}`)
.join("&");
// "q=hello%20world&page=1&lang=en"
// Parse query parameters from a URL
const url = new URL("https://example.com?q=hello%20world&page=1");
const q = url.searchParams.get("q"); // "hello world"
// Using URLSearchParams for encoding
const search = new URLSearchParams({ q: "café", sort: "name" });
search.toString(); // "q=caf%C3%A9&sort=name"
// Encode a full URI (preserves :, /, ?, #, etc.)
const fullUri = encodeURI("https://example.com/path?q=hello world");
// "https://example.com/path?q=hello%20world"
from urllib.parse import quote, unquote, urlencode, parse_qs
# URL encode a string
encoded = quote("hello world & foo=bar")
# "hello%20world%20%26%20foo%3Dbar"
# URL decode a string
decoded = unquote("hello%20world%20%26%20foo%3Dbar")
# "hello world & foo=bar"
# Encode special characters (safe="" encodes everything)
quote("price=100€&tax=20%", safe="")
# "price%3D100%E2%82%AC%26tax%3D20%25"
# Build query parameters from a dict
params = {"q": "hello world", "page": "1", "lang": "en"}
query = urlencode(params)
# "q=hello+world&page=1&lang=en"
# Use quote_plus for application/x-www-form-urlencoded
from urllib.parse import quote_plus
quote_plus("hello world") # "hello+world"
# Parse query parameters from a string
parsed = parse_qs("q=hello+world&page=1&lang=en")
# {"q": ["hello world"], "page": ["1"], "lang": ["en"]}
# Encode with specific safe characters
quote("https://example.com/path?q=test", safe=":/?=")
# "https://example.com/path?q=test"
# Handle Unicode characters
quote("café résumé", safe="")
# "caf%C3%A9%20r%C3%A9sum%C3%A9"
from urllib.parse import quote, unquote, urlencode, parse_qs
# URL encode a string
encoded = quote("hello world & foo=bar")
# "hello%20world%20%26%20foo%3Dbar"
# URL decode a string
decoded = unquote("hello%20world%20%26%20foo%3Dbar")
# "hello world & foo=bar"
# Encode special characters (safe="" encodes everything)
quote("price=100€&tax=20%", safe="")
# "price%3D100%E2%82%AC%26tax%3D20%25"
# Build query parameters from a dict
params = {"q": "hello world", "page": "1", "lang": "en"}
query = urlencode(params)
# "q=hello+world&page=1&lang=en"
# Use quote_plus for application/x-www-form-urlencoded
from urllib.parse import quote_plus
quote_plus("hello world") # "hello+world"
# Parse query parameters from a string
parsed = parse_qs("q=hello+world&page=1&lang=en")
# {"q": ["hello world"], "page": ["1"], "lang": ["en"]}
# Encode with specific safe characters
quote("https://example.com/path?q=test", safe=":/?=")
# "https://example.com/path?q=test"
# Handle Unicode characters
quote("café résumé", safe="")
# "caf%C3%A9%20r%C3%A9sum%C3%A9"
// Type-safe URL encoding utilities
// URL encode a string
const encoded: string = encodeURIComponent("hello world & foo=bar");
// "hello%20world%20%26%20foo%3Dbar"
// URL decode with error handling
function safeDecode(input: string): string | null {
try {
return decodeURIComponent(input);
} catch {
return null; // Invalid percent-encoded sequence
}
}
// Build type-safe query parameters
function buildQuery(params: Record<string, string>): string {
return Object.entries(params)
.map(([k, v]) => `${encodeURIComponent(k)}=${encodeURIComponent(v)}`)
.join("&");
}
const query = buildQuery({ q: "hello world", page: "1" });
// "q=hello%20world&page=1"
// Parse query string to typed object
function parseQuery(search: string): Record<string, string> {
const params = new URLSearchParams(search);
return Object.fromEntries(params.entries());
}
const parsed = parseQuery("q=hello%20world&page=1");
// { q: "hello world", page: "1" }
// Encode special characters for URL paths
const pathSegment = encodeURIComponent("docs/getting started");
const url = `https://example.com/${pathSegment}`;
// "https://example.com/docs%2Fgetting%20started"
// Using URL and URLSearchParams
const fullUrl = new URL("https://example.com/search");
fullUrl.searchParams.set("q", "café & thé");
fullUrl.toString();
// "https://example.com/search?q=caf%C3%A9+%26+th%C3%A9"
// Type-safe URL encoding utilities
// URL encode a string
const encoded: string = encodeURIComponent("hello world & foo=bar");
// "hello%20world%20%26%20foo%3Dbar"
// URL decode with error handling
function safeDecode(input: string): string | null {
try {
return decodeURIComponent(input);
} catch {
return null; // Invalid percent-encoded sequence
}
}
// Build type-safe query parameters
function buildQuery(params: Record<string, string>): string {
return Object.entries(params)
.map(([k, v]) => `${encodeURIComponent(k)}=${encodeURIComponent(v)}`)
.join("&");
}
const query = buildQuery({ q: "hello world", page: "1" });
// "q=hello%20world&page=1"
// Parse query string to typed object
function parseQuery(search: string): Record<string, string> {
const params = new URLSearchParams(search);
return Object.fromEntries(params.entries());
}
const parsed = parseQuery("q=hello%20world&page=1");
// { q: "hello world", page: "1" }
// Encode special characters for URL paths
const pathSegment = encodeURIComponent("docs/getting started");
const url = `https://example.com/${pathSegment}`;
// "https://example.com/docs%2Fgetting%20started"
// Using URL and URLSearchParams
const fullUrl = new URL("https://example.com/search");
fullUrl.searchParams.set("q", "café & thé");
fullUrl.toString();
// "https://example.com/search?q=caf%C3%A9+%26+th%C3%A9"
import java.net.URLEncoder;
import java.net.URLDecoder;
import java.net.URI;
import java.nio.charset.StandardCharsets;
public class UrlEncodingExample {
public static void main(String[] args) throws Exception {
// URL encode a string
String encoded = URLEncoder.encode(
"hello world & foo=bar", StandardCharsets.UTF_8
);
// "hello+world+%26+foo%3Dbar"
// URL decode a string
String decoded = URLDecoder.decode(
"hello+world+%26+foo%3Dbar", StandardCharsets.UTF_8
);
// "hello world & foo=bar"
// Encode special characters
URLEncoder.encode("price=100€&tax=20%", StandardCharsets.UTF_8);
// "price%3D100%E2%82%AC%26tax%3D20%25"
// Build query parameters
String query = String.join("&",
"q=" + URLEncoder.encode("hello world", StandardCharsets.UTF_8),
"page=" + URLEncoder.encode("1", StandardCharsets.UTF_8),
"lang=" + URLEncoder.encode("en", StandardCharsets.UTF_8)
);
// "q=hello+world&page=1&lang=en"
// Parse a URI and extract query parameters
URI uri = new URI("https://example.com?q=hello%20world&page=1");
String rawQuery = uri.getRawQuery(); // "q=hello%20world&page=1"
// Handle Unicode characters
String unicode = URLEncoder.encode("café résumé", StandardCharsets.UTF_8);
// "caf%C3%A9+r%C3%A9sum%C3%A9"
}
}
import java.net.URLEncoder;
import java.net.URLDecoder;
import java.net.URI;
import java.nio.charset.StandardCharsets;
public class UrlEncodingExample {
public static void main(String[] args) throws Exception {
// URL encode a string
String encoded = URLEncoder.encode(
"hello world & foo=bar", StandardCharsets.UTF_8
);
// "hello+world+%26+foo%3Dbar"
// URL decode a string
String decoded = URLDecoder.decode(
"hello+world+%26+foo%3Dbar", StandardCharsets.UTF_8
);
// "hello world & foo=bar"
// Encode special characters
URLEncoder.encode("price=100€&tax=20%", StandardCharsets.UTF_8);
// "price%3D100%E2%82%AC%26tax%3D20%25"
// Build query parameters
String query = String.join("&",
"q=" + URLEncoder.encode("hello world", StandardCharsets.UTF_8),
"page=" + URLEncoder.encode("1", StandardCharsets.UTF_8),
"lang=" + URLEncoder.encode("en", StandardCharsets.UTF_8)
);
// "q=hello+world&page=1&lang=en"
// Parse a URI and extract query parameters
URI uri = new URI("https://example.com?q=hello%20world&page=1");
String rawQuery = uri.getRawQuery(); // "q=hello%20world&page=1"
// Handle Unicode characters
String unicode = URLEncoder.encode("café résumé", StandardCharsets.UTF_8);
// "caf%C3%A9+r%C3%A9sum%C3%A9"
}
}
package main
import (
"fmt"
"net/url"
)
func main() {
// URL encode a string
encoded := url.QueryEscape("hello world & foo=bar")
// "hello+world+%26+foo%3Dbar"
// URL decode a string
decoded, err := url.QueryUnescape("hello+world+%26+foo%3Dbar")
if err != nil {
fmt.Println("Decode error:", err)
}
// "hello world & foo=bar"
// Encode path segments (spaces become %20, not +)
pathEncoded := url.PathEscape("docs/getting started")
// "docs%2Fgetting%20started"
// Build query parameters using url.Values
params := url.Values{}
params.Set("q", "hello world")
params.Set("page", "1")
params.Set("lang", "en")
query := params.Encode()
// "lang=en&page=1&q=hello+world"
// Parse query parameters from a URL
u, _ := url.Parse("https://example.com?q=hello%20world&page=1")
q := u.Query().Get("q") // "hello world"
// Build a complete URL with encoded components
base, _ := url.Parse("https://example.com/search")
base.RawQuery = params.Encode()
fullUrl := base.String()
// "https://example.com/search?lang=en&page=1&q=hello+world"
// Handle Unicode characters
unicode := url.QueryEscape("café résumé")
// "caf%C3%A9+r%C3%A9sum%C3%A9"
fmt.Println(encoded, decoded, pathEncoded, query, q, fullUrl, unicode)
}
package main
import (
"fmt"
"net/url"
)
func main() {
// URL encode a string
encoded := url.QueryEscape("hello world & foo=bar")
// "hello+world+%26+foo%3Dbar"
// URL decode a string
decoded, err := url.QueryUnescape("hello+world+%26+foo%3Dbar")
if err != nil {
fmt.Println("Decode error:", err)
}
// "hello world & foo=bar"
// Encode path segments (spaces become %20, not +)
pathEncoded := url.PathEscape("docs/getting started")
// "docs%2Fgetting%20started"
// Build query parameters using url.Values
params := url.Values{}
params.Set("q", "hello world")
params.Set("page", "1")
params.Set("lang", "en")
query := params.Encode()
// "lang=en&page=1&q=hello+world"
// Parse query parameters from a URL
u, _ := url.Parse("https://example.com?q=hello%20world&page=1")
q := u.Query().Get("q") // "hello world"
// Build a complete URL with encoded components
base, _ := url.Parse("https://example.com/search")
base.RawQuery = params.Encode()
fullUrl := base.String()
// "https://example.com/search?lang=en&page=1&q=hello+world"
// Handle Unicode characters
unicode := url.QueryEscape("café résumé")
// "caf%C3%A9+r%C3%A9sum%C3%A9"
fmt.Println(encoded, decoded, pathEncoded, query, q, fullUrl, unicode)
}
<?php
// URL encode a string (spaces become +)
$encoded = urlencode("hello world & foo=bar");
// "hello+world+%26+foo%3Dbar"
// URL decode a string
$decoded = urldecode("hello+world+%26+foo%3Dbar");
// "hello world & foo=bar"
// Raw URL encode (spaces become %20, RFC 3986)
$raw = rawurlencode("hello world & foo=bar");
// "hello%20world%20%26%20foo%3Dbar"
// Raw URL decode
$rawDecoded = rawurldecode("hello%20world%20%26%20foo%3Dbar");
// "hello world & foo=bar"
// Encode special characters
rawurlencode("price=100€&tax=20%");
// "price%3D100%E2%82%AC%26tax%3D20%25"
// Build query parameters from an array
$params = ["q" => "hello world", "page" => "1", "lang" => "en"];
$query = http_build_query($params);
// "q=hello+world&page=1&lang=en"
// Parse query parameters into an array
parse_str("q=hello+world&page=1&lang=en", $result);
// $result = ["q" => "hello world", "page" => "1", "lang" => "en"]
// Encode a full URL (only unsafe characters)
$url = "https://example.com/path?q=" . urlencode("café");
// "https://example.com/path?q=caf%C3%A9"
// Handle arrays in query parameters
$arrayParams = ["colors" => ["red", "blue"]];
$arrayQuery = http_build_query($arrayParams);
// "colors%5B0%5D=red&colors%5B1%5D=blue"
<?php
// URL encode a string (spaces become +)
$encoded = urlencode("hello world & foo=bar");
// "hello+world+%26+foo%3Dbar"
// URL decode a string
$decoded = urldecode("hello+world+%26+foo%3Dbar");
// "hello world & foo=bar"
// Raw URL encode (spaces become %20, RFC 3986)
$raw = rawurlencode("hello world & foo=bar");
// "hello%20world%20%26%20foo%3Dbar"
// Raw URL decode
$rawDecoded = rawurldecode("hello%20world%20%26%20foo%3Dbar");
// "hello world & foo=bar"
// Encode special characters
rawurlencode("price=100€&tax=20%");
// "price%3D100%E2%82%AC%26tax%3D20%25"
// Build query parameters from an array
$params = ["q" => "hello world", "page" => "1", "lang" => "en"];
$query = http_build_query($params);
// "q=hello+world&page=1&lang=en"
// Parse query parameters into an array
parse_str("q=hello+world&page=1&lang=en", $result);
// $result = ["q" => "hello world", "page" => "1", "lang" => "en"]
// Encode a full URL (only unsafe characters)
$url = "https://example.com/path?q=" . urlencode("café");
// "https://example.com/path?q=caf%C3%A9"
// Handle arrays in query parameters
$arrayParams = ["colors" => ["red", "blue"]];
$arrayQuery = http_build_query($arrayParams);
// "colors%5B0%5D=red&colors%5B1%5D=blue"
require 'uri'
require 'cgi'
# URL encode a string (spaces become %20)
encoded = URI.encode_www_form_component("hello world & foo=bar")
# "hello+world+%26+foo%3Dbar"
# URL decode a string
decoded = URI.decode_www_form_component("hello+world+%26+foo%3Dbar")
# "hello world & foo=bar"
# Encode special characters
URI.encode_www_form_component("price=100€&tax=20%")
# "price%3D100%E2%82%AC%26tax%3D20%25"
# Build query parameters from a hash
params = { q: "hello world", page: "1", lang: "en" }
query = URI.encode_www_form(params)
# "q=hello+world&page=1&lang=en"
# Parse query parameters from a string
parsed = URI.decode_www_form("q=hello+world&page=1&lang=en")
# [["q", "hello world"], ["page", "1"], ["lang", "en"]]
hash = parsed.to_h
# {"q"=>"hello world", "page"=>"1", "lang"=>"en"}
# Parse a full URI and extract query params
uri = URI.parse("https://example.com?q=hello%20world&page=1")
CGI.parse(uri.query)
# {"q"=>["hello world"], "page"=>["1"]}
# Escape HTML entities in URLs (CGI module)
CGI.escape("café résumé")
# "caf%C3%A9+r%C3%A9sum%C3%A9"
require 'uri'
require 'cgi'
# URL encode a string (spaces become %20)
encoded = URI.encode_www_form_component("hello world & foo=bar")
# "hello+world+%26+foo%3Dbar"
# URL decode a string
decoded = URI.decode_www_form_component("hello+world+%26+foo%3Dbar")
# "hello world & foo=bar"
# Encode special characters
URI.encode_www_form_component("price=100€&tax=20%")
# "price%3D100%E2%82%AC%26tax%3D20%25"
# Build query parameters from a hash
params = { q: "hello world", page: "1", lang: "en" }
query = URI.encode_www_form(params)
# "q=hello+world&page=1&lang=en"
# Parse query parameters from a string
parsed = URI.decode_www_form("q=hello+world&page=1&lang=en")
# [["q", "hello world"], ["page", "1"], ["lang", "en"]]
hash = parsed.to_h
# {"q"=>"hello world", "page"=>"1", "lang"=>"en"}
# Parse a full URI and extract query params
uri = URI.parse("https://example.com?q=hello%20world&page=1")
CGI.parse(uri.query)
# {"q"=>["hello world"], "page"=>["1"]}
# Escape HTML entities in URLs (CGI module)
CGI.escape("café résumé")
# "caf%C3%A9+r%C3%A9sum%C3%A9"
using System;
using System.Net;
using System.Web;
using System.Collections.Specialized;
// URL encode a string
string encoded = Uri.EscapeDataString("hello world & foo=bar");
// "hello%20world%20%26%20foo%3Dbar"
// URL decode a string
string decoded = Uri.UnescapeDataString("hello%20world%20%26%20foo%3Dbar");
// "hello world & foo=bar"
// Encode special characters
Uri.EscapeDataString("price=100€&tax=20%");
// "price%3D100%E2%82%AC%26tax%3D20%25"
// Alternative: WebUtility (spaces become +)
WebUtility.UrlEncode("hello world & foo=bar");
// "hello+world+%26+foo%3Dbar"
WebUtility.UrlDecode("hello+world+%26+foo%3Dbar");
// "hello world & foo=bar"
// Build query parameters
var query = string.Join("&",
$"q={Uri.EscapeDataString("hello world")}",
$"page={Uri.EscapeDataString("1")}",
$"lang={Uri.EscapeDataString("en")}"
);
// "q=hello%20world&page=1&lang=en"
// Parse query parameters from a URL
var uri = new Uri("https://example.com?q=hello%20world&page=1");
NameValueCollection queryParams = HttpUtility.ParseQueryString(uri.Query);
string q = queryParams["q"]; // "hello world"
// Handle Unicode characters
Uri.EscapeDataString("café résumé");
// "caf%C3%A9%20r%C3%A9sum%C3%A9"
using System;
using System.Net;
using System.Web;
using System.Collections.Specialized;
// URL encode a string
string encoded = Uri.EscapeDataString("hello world & foo=bar");
// "hello%20world%20%26%20foo%3Dbar"
// URL decode a string
string decoded = Uri.UnescapeDataString("hello%20world%20%26%20foo%3Dbar");
// "hello world & foo=bar"
// Encode special characters
Uri.EscapeDataString("price=100€&tax=20%");
// "price%3D100%E2%82%AC%26tax%3D20%25"
// Alternative: WebUtility (spaces become +)
WebUtility.UrlEncode("hello world & foo=bar");
// "hello+world+%26+foo%3Dbar"
WebUtility.UrlDecode("hello+world+%26+foo%3Dbar");
// "hello world & foo=bar"
// Build query parameters
var query = string.Join("&",
$"q={Uri.EscapeDataString("hello world")}",
$"page={Uri.EscapeDataString("1")}",
$"lang={Uri.EscapeDataString("en")}"
);
// "q=hello%20world&page=1&lang=en"
// Parse query parameters from a URL
var uri = new Uri("https://example.com?q=hello%20world&page=1");
NameValueCollection queryParams = HttpUtility.ParseQueryString(uri.Query);
string q = queryParams["q"]; // "hello world"
// Handle Unicode characters
Uri.EscapeDataString("café résumé");
// "caf%C3%A9%20r%C3%A9sum%C3%A9"