Windows 2008 Server に cURL をインストールし、以下のコマンドを実行しようとしています。このコマンドは同じ LAN 上の Ubuntu マシンでは正常に実行されますが、Windows で実行すると次のエラーが発生します。
curl -H "Content-Type: application/json" -X POST -d '{ "entity_id": "switch.study_cam" }' https://192.168.1.99:8123/api/services/switch/turn_off?api_password=MyAPIPassword --insecure
curl: (6) Could not resolve host: entity_id
curl: (6) Could not resolve host: switch.study_cam
curl: (3) [globbing] unmatched close brace/bracket in column 1
{"message": "Data should be valid JSON"}
私はWindowsでcURLをテストしましたhttp://www.google.com 有効な HTML が返されたので、正しくインストールされたようです。
Windows と Linux の cURL には構文の違いがありますか? または、上記のコマンドが Windows で失敗する理由について他の説明がありますか?
答え1
問題はおそらく、Windows コマンド プロンプトが一重引用符と二重引用符を異なる方法で解釈することであり、curl とは関係ありません。
JSON セクションの二重引用符と一重引用符を逆にしてみてください。
curl -H "Content-Type: application/json" -X POST -d "{ 'entity_id': 'switch.study_cam' }" https://192.168.1.99:8123/api/services/switch/turn_off?api_password=MyAPIPassword --insecure
答え2
私は JSON をファイル (例: ) に入れてjson.txt
、 を使用することでcurl -d @json.txt
、シェルによる引用符処理の問題を回避します。
これは次のようなものになります
curl -H "Content-Type: application/json" -X POST -d @json.txt \
https://192.168.1.99:8123/api/services/switch\
/turn_off?api_password=MyAPIPassword --insecure
Unixシェルが恋しいなら、MSYS2は、Windows マシンに通常のツール群を追加するための優れたシステムです (ただし、Windows XP / Windows Server 2003 より新しいバージョンである必要があります)。