2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

症状:バッチファイルでcurlコマンドがエラーになる。原因:URLにコマンドライン引数を含んでいるため

Last updated at Posted at 2024-11-01

症状

curlコマンドで以下URLを叩いたところ、バッチファイルから起動した場合とコマンドプロンプト直打ちの場合とで結果異なる。

bad_pattern.cmd
curl -v "https://www.google.com/search?q=test%252sample"

バッチファイルから実行した結果

curl -v "https://www.google.com/search?q=test%%252sample" |find  "GET"    
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0* Host www.google.com:443 was resolved.
* IPv6: (none)
* Connected to www.google.com (142.250.198.4) port 443
* schannel: disabled automatic use of client certificate
* ALPN: curl offers http/1.1
* ALPN: server accepted http/1.1
* using HTTP/1.x
+ > GET /search?q=test%252sample HTTP/1.1

コマンドを直に実行した場合

>bad_pattern.cmd
>curl -v "https://www.google.com/search?q=test52sample" 
* Host www.google.com:443 was resolved.
* IPv6: (none)
* IPv4: 142.250.198.4
*   Trying 142.250.198.4:443...
* Connected to www.google.com (142.250.198.4) port 443
* schannel: disabled automatic use of client certificate
* ALPN: curl offers http/1.1
* ALPN: server accepted http/1.1
* using HTTP/1.x
+ > GET /search?q=test52sample HTTP/1.1

原因

バッチファイル内では「%2」はコマンドライン引数であるため%2=""となり求めるURLにならない

対処方法

バッチファイルを以下のように修正する。

- curl -v "https://www.google.com/search?q=test%252sample"
+ curl -v "https://www.google.com/search?q=test%%252sample"

一言

これだけで2時間ぐらい潰れました。
メモ帳で編集していたのが長引いた原因でした。
VSCODE使っていれば、シンタックスハイライトでもうちょっと早く気づけたと思います。

image.png

参考

引数の指定とバッチファイル内で引数を参照する方法
https://www.javadrive.jp/command/bat/index6.html

image.png

2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?