URL 编码将字符转换为可以通过互联网传输的格式。保留字符和非 ASCII 字符会被替换为百分号后跟两个十六进制数字的形式。
当需要在 URL 中安全包含特殊字符(如空格、?、& 或 =)时,应使用 URL 编码。否则,URL 可能因保留字符而发生错误或被误读。
# Encoding
printf 'Hello, World!' | jq -sRr @uri
# Output: Hello%2C%20World%21
# Decoding
printf 'Hello%2C%20World%21' | jq -sRr @urid
# Output: Hello, World!
// Encoding
let encoded = encodeURIComponent("Hello, World!");
console.log(encoded);
// Output: Hello%2C%20World%21
// Decoding
let decoded = decodeURIComponent("Hello%2C%20World%21");
console.log(decoded);
// Output: Hello, World!
package main
import (
"fmt"
"net/url"
)
func main() {
// Encoding
encoded := url.QueryEscape("Hello, World!")
fmt.Println(encoded)
// Output: Hello%2C+%21World%21 (QueryEscape uses + for spaces)
// Decoding
decoded, _ := url.QueryUnescape("Hello%2C%20World%21")
fmt.Println(decoded)
// Output: Hello, World!
}
// Encoding
$encoded = urlencode("Hello, World!");
echo $encoded . "\n";
// Output: Hello%2C+%21World%21
// Decoding
$decoded = urldecode("Hello%2C%20World%21");
echo $decoded . "\n";
// Output: Hello, World!