こんにちは。たいら(@tairaengineer2)です。
転職を繰り返し現在5社経験している、14年目エンジニアです。
この記事では、curlコマンドを実行したとき
1 |
curl -i -X GET https://httpbin.org/get?hogehoge=test |
というようにクエリ文字列に「&」が付いていない場合は
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
curl -i -X GET https://httpbin.org/get?hogehoge=test HTTP/2 200 date: Sat, 17 May 2025 06:59:20 GMT content-type: application/json content-length: 295 server: gunicorn/19.9.0 access-control-allow-origin: * access-control-allow-credentials: true { "args": { "hogehoge": "test" }, "headers": { "Accept": "*/*", "Host": "httpbin.org", "User-Agent": "curl/7.81.0", "X-Amzn-Trace-Id": "Root=1-682833c8-0b4e5a4716bc5dad12add0de" }, "origin": "xxx.xxx.xxx.xxx", "url": "https://httpbin.org/get?hogehoge=test" } |
正常に動くのに
1 |
curl -i -X GET https://httpbin.org/get?hogehoge=test&fugafuga=sample |
というようにクエリ文字列に「&」が付いている場合は
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
[1] 7743 date: Sat, 17 May 2025 07:04:43 GMT content-type: application/json content-length: 295 server: gunicorn/19.9.0 access-control-allow-origin: * access-control-allow-credentials: true { "args": { "hogehoge": "test" }, "headers": { "Accept": "*/*", "Host": "httpbin.org", "User-Agent": "curl/7.81.0", "X-Amzn-Trace-Id": "Root=1-6828350b-067c115203d9e9b612bd1703" }, "origin": "xxx.xxx.xxx.xxx", "url": "https://httpbin.org/get?hogehoge=test" } |
クエリ文字列に指定した2つ目が認識されていない、そもそも止まらない、とりあえず、想定通りではない動きをしたときの原因とその解決策をお伝えします。
(↑の止まらない状態は、ctrlキー+cキーで止まります。)
「&」付きでクエリ文字列を指定して投げると失敗する原因
おかしな動きをする原因は、「&」が原因です。
「&」はLinuxでもWindowsでもコマンドとして意味を持っています。
なので
1 |
curl -i -X GET https://httpbin.org/get?hogehoge=test&fugafuga=sample |
のコマンドは
1 |
curl -i -X GET https://httpbin.org/get?hogehoge=test |
までしか解釈されず、「&」が悪さをして想定通りの動きをしませんでした。
「&」付きでクエリ文字列を指定して投げると失敗する解決策
2つあります。
1つはクエリ文字列ごとURLをまるっと「”(ダブルクオーテーション)」で囲むことです。
先ほどのcurlコマンドだと
1 |
curl -i -X GET "https://httpbin.org/get?hogehoge=test&fugafuga=sample" |
になり、実行すると2つ目も認識されて実行することができます。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
HTTP/2 200 date: Sat, 17 May 2025 07:42:46 GMT content-type: application/json content-length: 338 server: gunicorn/19.9.0 access-control-allow-origin: * access-control-allow-credentials: true { "args": { "fugafuga": "sample", "hogehoge": "test" }, "headers": { "Accept": "*/*", "Host": "httpbin.org", "User-Agent": "curl/7.81.0", "X-Amzn-Trace-Id": "Root=1-68283df5-69247bbc1c4936d268c32542" }, "origin": "xxx.xxx.xxx.xxx", "url": "https://httpbin.org/get?hogehoge=test&fugafuga=sample" } |
もう1つはエスケープすることです
先ほどのcurlコマンドだと
1 |
curl -i -X GET https://httpbin.org/get?hogehoge=test\&fugafuga=sample |
になり、実行するとこちらも2つ目も認識されて実行することができます。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
HTTP/2 200 date: Sat, 17 May 2025 07:48:01 GMT content-type: application/json content-length: 338 server: gunicorn/19.9.0 access-control-allow-origin: * access-control-allow-credentials: true { "args": { "fugafuga": "sample", "hogehoge": "test" }, "headers": { "Accept": "*/*", "Host": "httpbin.org", "User-Agent": "curl/7.81.0", "X-Amzn-Trace-Id": "Root=1-68283f31-1ef571606572bd375093e3b5" }, "origin": "220.147.228.6", "url": "https://httpbin.org/get?hogehoge=test&fugafuga=sample" } |
まとめ:「&」付きでクエリ文字列を指定して投げると失敗したときはエスケープしよう!
以上が「&」付きでクエリ文字列を指定して投げると失敗する原因と解決策でした!
あなたのご参考になったのなら、とても嬉しいです(*´▽`*)
ではでは~(・ω・)ノシ
コメント