URL エンコードで日本語と空白は Python・JS・Go でどう違うか

要点

  • 日本語の部分は、JS の escape 以外のどの関数でも同じ UTF-8 の %XX になり、違ったのは空白の扱いだけだった(出典: 自社の実測・2026-10-20)
  • 空白を %20 にしたのは Python quote、JS encodeURI / encodeURIComponent、Go PathEscape だった(出典: 自社の実測・2026-10-20)
  • 空白を + にしたのは Python quote_plus / urlencode、JS URLSearchParams、Go QueryEscape / Values.Encode だった(出典: 自社の実測・2026-10-20)
  • + を空白に戻すのはクエリ用の関数だけで、unquote、decodeURIComponent、PathUnescape は + のまま返した(出典: 自社の実測・2026-10-20)
  • encodeURI() は ; / ? : @ & = + $ , # をエスケープしないと MDN に書かれている(出典: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURI )

何が困るのか

この記事でいう URL エンコードとは、文字列を %XX の並びに置き換える関数(Python の quote、JavaScript の encodeURIComponent、Go の url.QueryEscape など)の処理を指す。

困るのは、同じ「URL エンコードする関数」でも言語と関数によって結果が違うことだ。
検索語の 日本語 テスト をクエリに入れるとき、空白は %20 になる場合と + になる場合がある。
+ や & や / がそのまま残る関数もあれば、%2B や %26 や %2F にする関数もある。

さらに戻す側にも差がある。
+ で空白を表した値を、+ を空白に戻さない関数で戻すと、空白が + のまま残る。

そこで、同じ5つの入力を3言語の関数に通して出力を並べた。
入力は "日本語 テスト"(間は半角空白)、"a+b&c=d"、"~!*'()"、"/path/to?x=1"、"a b" の5つである。

測った環境は macOS 26.5.1(arm64)、Python 3.9.0、Node v22.22.3、Go 1.26.2 で、出力はそのまま写した。

出典: 自社の実測(2026-10-20)

公式はどう書いているか

encodeURIComponent とは、A–Z a–z 0–9 - _ . ! ~ * ' ( ) 以外のすべての文字をエスケープする JavaScript の関数だ。
MDN の記述は次のとおり。

encodeURIComponent() uses the same encoding algorithm as described in encodeURI() . It escapes all characters except : A–Z a–z 0–9 – _ . ! ~ * ' ( ) Compared to encodeURI() , encodeURIComponent() escapes a larger set of characters.

出典: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent (取得日 2026-10-20)

encodeURI は、それより多くの記号を残す。

encodeURI() escapes all characters except : A–Z a–z 0–9 – _ . ! ~ * ' ( ) ; / ? : @ & = + $ , # The characters on the second line are characters that may be part of the URI syntax, and are only escaped by encodeURIComponent() .

出典: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURI (取得日 2026-10-20)

URLSearchParams は、空白を + にすると明記されている。

URLSearchParams objects percent-encode anything in the application/x-www-form-urlencoded percent-encode set (which contains all code points except ASCII alphanumeric, * , – , . , and _ ), and encode U+0020 SPACE as + .

出典: https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams (取得日 2026-10-20)

Python は、Python 3.9.0 の docstring(2026-10-20 に取得)にこう書かれている。

  • quote: "The default for the safe arg is '/'. The character is reserved, but in typical usage the quote function is being called on a path where the existing slash characters are to be preserved."
  • quote: "By default, encoding='utf-8' (characters are encoded with UTF-8), and errors='strict' (unsupported characters raise a UnicodeEncodeError)."
  • quote_plus: "Like quote(), but also replace ' ' with '+', as required for quoting HTML form values. Plus signs in the original string are escaped unless they are included in safe. It also does not have safe default to '/'."

Go は、Go 1.26.2 の go doc(2026-10-20 に取得)にこう書かれている。

  • QueryEscape: "QueryEscape escapes the string so it can be safely placed inside a URL query."
  • PathEscape: "PathEscape escapes the string so it can be safely placed inside a URL path segment, replacing special characters (including /) with %XX sequences as needed."

Go の2つの説明文には、空白を %20 にするか + にするかは書かれていない。
そこは次の節の実行結果で見る。

実行して比べた表

まず "日本語 テスト" の結果である(macOS 26.5.1・Python 3.9.0・Node v22.22.3・Go 1.26.2)。

関数 出力
Python quote(s) %E6%97%A5%E6%9C%AC%E8%AA%9E%20%E3%83%86%E3%82%B9%E3%83%88
Python quote_plus(s) %E6%97%A5%E6%9C%AC%E8%AA%9E+%E3%83%86%E3%82%B9%E3%83%88
Python urlencode({'q': s}) q=%E6%97%A5%E6%9C%AC%E8%AA%9E+%E3%83%86%E3%82%B9%E3%83%88
JS encodeURI(s) %E6%97%A5%E6%9C%AC%E8%AA%9E%20%E3%83%86%E3%82%B9%E3%83%88
JS encodeURIComponent(s) %E6%97%A5%E6%9C%AC%E8%AA%9E%20%E3%83%86%E3%82%B9%E3%83%88
JS new URLSearchParams({q: s}).toString() q=%E6%97%A5%E6%9C%AC%E8%AA%9E+%E3%83%86%E3%82%B9%E3%83%88
JS escape(s) %u65E5%u672C%u8A9E%20%u30C6%u30B9%u30C8
Go url.QueryEscape(s) %E6%97%A5%E6%9C%AC%E8%AA%9E+%E3%83%86%E3%82%B9%E3%83%88
Go url.PathEscape(s) %E6%97%A5%E6%9C%AC%E8%AA%9E%20%E3%83%86%E3%82%B9%E3%83%88
Go url.Values{"q": {s}}.Encode() q=%E6%97%A5%E6%9C%AC%E8%AA%9E+%E3%83%86%E3%82%B9%E3%83%88

日本語の部分は、escape 以外すべて同じ UTF-8 の %XX だった。
違ったのは空白だけである。
JS の escape は日本語を %uXXXX の形にしており、UTF-8 の %XX ではなかった。

次に記号と空白の結果である。

入力 Python quote(s) Python quote(s, safe='') Python quote_plus(s) JS encodeURI(s) JS encodeURIComponent(s) JS URLSearchParams Go QueryEscape Go PathEscape
a+b&c=d a%2Bb%26c%3Dd a%2Bb%26c%3Dd a%2Bb%26c%3Dd a+b&c=d a%2Bb%26c%3Dd q=a%2Bb%26c%3Dd a%2Bb%26c%3Dd a+b&c=d
~!*'() ~%21%2A%27%28%29 ~%21%2A%27%28%29 ~%21%2A%27%28%29 ~!*'() ~!*'() q=%7E%21*%27%28%29 ~%21%2A%27%28%29 ~%21%2A%27%28%29
/path/to?x=1 /path/to%3Fx%3D1 %2Fpath%2Fto%3Fx%3D1 %2Fpath%2Fto%3Fx%3D1 /path/to?x=1 %2Fpath%2Fto%3Fx%3D1 q=%2Fpath%2Fto%3Fx%3D1 %2Fpath%2Fto%3Fx%3D1 %2Fpath%2Fto%3Fx=1
a b a%20b a%20b a+b a%20b a%20b q=a+b a+b a%20b

この表から読めることは4つある。

  • Python の quote は既定で / を残した。safe='' を渡すと %2F になった。docstring の「safe の既定は '/'」と一致する。
  • JS の encodeURI は + & = / ? を残した。MDN の記述と一致する。
  • ~!*'() を全部残したのは JS の encodeURI と encodeURIComponent だけだった。Python quote と Go は ~ だけ残し、JS URLSearchParams は * だけ残した(~ は %7E)。
  • Go の PathEscape は + & = を残し、/ と ? は %2F と %3F にした。

出典: 自社の実測(2026-10-20)

どれを使うか

表と公式の記述から言える範囲で整理する。

クエリの値を1つ作るときに encodeURI を使うと、a+b&c=d がそのまま入る。
& と = が残るので、値の中の記号がクエリの区切りと同じ文字のままになる。
同じ入力を encodeURIComponent に通すと a%2Bb%26c%3Dd になった。

Python の quote は、docstring が「パスに対して呼ばれ、既にあるスラッシュを残す」使い方を典型として挙げている。
/ も変えたいときは safe='' を渡すか、safe の既定が '/' ではない quote_plus を使う。
ただし quote_plus は空白を + にする。

Go は go doc の説明どおり、クエリには QueryEscape、パスの1区切りには PathEscape が用意されている。
実行結果では、前者は空白を + に、後者は %20 にした。

キー=値 の形ごと作る関数(Python urlencode、JS URLSearchParams、Go Values.Encode)は、3言語とも空白を + にした。

出典: 自社の実測(2026-10-20)

落とし穴: 戻す側で + が空白に化ける関数、化けない関数

戻す側の関数に、3つの入力を通した結果である。

入力 Python unquote Python unquote_plus Python parse_qs JS decodeURIComponent JS URLSearchParams.get Go QueryUnescape Go PathUnescape Go ParseQuery
a+b 'a+b' 'a b' ['a b'] "a+b" "a b" "a b" "a+b" "a b"
a%20b 'a b' 'a b' ['a b'] "a b" "a b" "a b" "a b" "a b"
a%2Bb 'a+b' 'a+b' ['a+b'] "a+b" "a+b" "a+b" "a+b" "a+b"

+ を空白に戻したのは、クエリ用の関数だけだった。
unquote_plus、parse_qs、URLSearchParams、QueryUnescape、ParseQuery の5つである。
unquote、decodeURIComponent、PathUnescape は + を + のまま返した。

つまり quote_plus、URLSearchParams、QueryEscape で作った値を unquote、decodeURIComponent、PathUnescape で戻すと、空白が + のまま残る。
%2B にしておけば、どの関数でも + に戻った。

文字コードの落とし穴もある。
Python で quote('日本語', encoding='cp932') とすると %93%FA%96%7B%8C%EA になり、UTF-8 の %E6%97%A5… とは別のバイト列だった。
これを UTF-8 として戻したときの挙動は、言語で違った。

  • Node の decodeURIComponent("%93%FA%96%7B%8C%EA") は例外 URIError URI malformed になった。
  • Python の unquote('%93%FA%96%7B%8C%EA') は例外を出さず、置換文字 U+FFFD に化けた文字列を返した。unquote(..., encoding='cp932') とすると '日本語' に戻った。

確認できなかったことも書いておく。

  • ブラウザが URL バーやフォーム送信で実際に何を送るかは測っていない。測ったのは Node v22.22.3 の関数だけである。
  • Python の requests など外部ライブラリの既定は測っていない。
  • Python 3.9.0、Node v22.22.3、Go 1.26.2 以外の版は測っていない。
  • 全角スペース(U+3000)は入力に入れていない。

出典: 自社の実測(2026-10-20)

自分の環境で確かめる手順

版によって結果が同じとは限らないので、自分の環境で打って確かめてほしい。
先に版を取り、続けて空白と + の扱いを見る。

python3 -V; node -v; go version
python3 -c "from urllib.parse import quote, quote_plus, unquote; print(quote('a b'), quote_plus('a b'), unquote('a+b'))"
node -e "console.log(encodeURI('a+b&c=d'), encodeURIComponent('a+b&c=d'), new URLSearchParams({q:'a b'}).toString())"
python3 -I -c "from urllib.parse import quote, quote_plus; print(quote.__doc__); print(quote_plus.__doc__)"
go doc net/url.QueryEscape

Go の出力まで見たいときは、次の本体を main 関数に入れて go run する。

cases := []string{"日本語 テスト", "a+b&c=d", "~!*'()", "/path/to?x=1", "a b"}
for _, s := range cases {
	fmt.Printf("%q\n", s)
	fmt.Println("  url.QueryEscape(s)          ", url.QueryEscape(s))
	fmt.Println("  url.PathEscape(s)           ", url.PathEscape(s))
	fmt.Println("  url.Values{\"q\":{s}}.Encode()", url.Values{"q": {s}}.Encode())
}

見る点は3つある。
空白が %20 か + か、/ と & が残るか、戻したときに + が空白になるかである。

出典: 自社の実測(2026-10-20)

コメント

タイトルとURLをコピーしました