cURL 命令在 Linux 中運行,但在 Windows 2008 中運行

cURL 命令在 Linux 中運行,但在 Windows 2008 中運行

我已經在 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 上測試了 cURLhttp://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 它來避免 shell 的參考處理問題。

這會給類似的東西

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 shell,系統2是一個很好的系統,可以將常用的工具組新增至您的 Windows 電腦(但它必須比 Windows XP / Windows Server 2003 更新)。

相關內容