URL 编解码/ 编码 · 解码 · 表单编码

encodeURIComponent、encodeURI、表单编码三种模式,支持中文与特殊字符,实时编解码。

编码所有特殊字符(推荐,用于查询参数值)

原始文本
0 字符
编码结果
体积比: —%0 字符
常见字符编码对照
%20 / +
!%21
"%22
#%23
%%25
&%26
'%27
+%2B
/%2F
=%3D
?%3F
@%40

这个工具有帮到你吗?

什么是 URL 编码

URL 编码(Percent-encoding)将 URL 中的特殊字符转换为 `%` 加两位十六进制数的形式。URL 只允许使用字母、数字和少数符号(`-_.~`),其他字符如空格(%20)、中文(%E4%B8%AD)都需要编码。浏览器会自动编码地址栏中的 URL,但在程序中构建 URL 时需要手动处理。

URL 编码的常见场景

在查询参数中传递特殊字符(如搜索关键词包含 `&` 或 `=`)、表单提交数据(POST 默认使用 URL 编码)、API 请求中传递 JSON 或中文参数、处理 OAuth 回调 URL 等。编程语言中常用 `encodeURIComponent()`(JavaScript)或 `urllib.parse.quote()`(Python)进行编码。

代码示例

JavaScript
// Encode a query parameter value
encodeURIComponent("Hello World! 中文")
// "Hello%20World!%20%E4%B8%AD%E6%96%87"

// Build a full query string (recommended)
const params = new URLSearchParams({
  q: "hello world",
  lang: "zh-CN",
  page: "1",
});
params.toString()
// "q=hello+world&lang=zh-CN&page=1"

// Decode
decodeURIComponent("Hello%20World%21")
// "Hello World!"
Python
from urllib.parse import (
    quote, unquote,
    urlencode, quote_plus
)

# Encode single value
quote("Hello World! 中文")
# 'Hello%20World%21%20%E4%B8%AD%E6%96%87'

# Form encoding (spaces → +)
quote_plus("hello world")
# 'hello+world'

# Build query string
urlencode({"q": "hello world", "page": 1})
# 'q=hello+world&page=1'

# Decode
unquote("Hello%20World%21")  # 'Hello World!'
Go
import "net/url"

// Encode a path segment
url.PathEscape("hello world/中文")
// "hello%20world%2F%E4%B8%AD%E6%96%87"

// Encode a query value
url.QueryEscape("hello world")
// "hello+world"

// Build query string
params := url.Values{}
params.Set("q", "hello world")
params.Set("lang", "zh-CN")
params.Encode()
// "lang=zh-CN&q=hello+world"

// Parse a URL
u, _ := url.Parse("https://example.com/search?q=hello+world")
u.Query().Get("q")  // "hello world"
Shell (curl)
# curl handles encoding automatically with --data-urlencode
curl -G https://api.example.com/search \
  --data-urlencode "q=hello world 中文" \
  --data-urlencode "page=1"

# Manual percent-encoding with Python
python3 -c "
import sys
from urllib.parse import quote
print(quote(sys.stdin.read().strip()))
" <<< "hello world 中文"

# Using jq to build encoded JSON body
curl -X POST https://api.example.com \
  -H 'Content-Type: application/json' \
  -d "$(jq -n --arg q 'hello world' '{query: $q}')"

常见问题

encodeURIComponent 和 encodeURI 有什么区别?
encodeURIComponent 编码几乎所有特殊字符(包括 :/?#[]@!$&'()*+,;=),适合对单个查询参数值编码。encodeURI 保留 URL 的结构字符,适合对完整 URL 编码(如确保中文字符被转义,但不破坏 URL 结构)。绝大多数场景应使用 encodeURIComponent。
什么是表单编码(application/x-www-form-urlencoded)?
表单编码是 HTML 表单默认的提交格式,与 encodeURIComponent 的区别只有一点:空格被编码为 + 而不是 %20。浏览器在提交 <form> 时自动使用这种格式,许多 API 也接受这种格式的请求体(Content-Type: application/x-www-form-urlencoded)。
为什么 URL 中不能有中文?
URL 规范(RFC 3986)只允许 ASCII 字符。中文和其他非 ASCII 字符需要先用 UTF-8 编码为字节序列,再将每个字节表示为 %XX 格式。例如「中」的 UTF-8 编码是 0xE4 0xB8 0xAD,URL 编码后为 %E4%B8%AD。
哪些字符不需要 URL 编码?
RFC 3986 定义的「非保留字符」不需要编码:大小写字母(A-Z, a-z)、数字(0-9)和 - _ . ~ 四个符号。在具体的 URL 组件中(如路径、查询),还有一些允许出现的保留字符,但通常建议对所有非字母数字字符进行编码以确保兼容性。
%20 和 + 都表示空格,有什么区别?
%20 是通用的 URL 编码,在 URL 的任何位置都有效。+ 在 URL 路径中表示字面量加号,只有在 query string(? 之后)的 application/x-www-form-urlencoded 上下文中才表示空格。建议统一使用 %20 避免歧义。
如何在 JavaScript 中正确构建 URL 查询参数?
推荐使用 URLSearchParams API:new URLSearchParams({ key: value }).toString() 会自动进行正确的表单编码。或者手动拼接时对每个值使用 encodeURIComponent(value)。避免直接拼接未编码的字符串,容易产生注入漏洞或解析错误。