首页 > 解决方案 > cUrl 引发 JSON 格式错误但 Wget 不引发?

问题描述

我正在通过 Windows 上的批处理脚本发送 POST 请求。我手动安装了 wget,一切正常,但我想使用 cUrl,因为它最近成为 Windows 10 的标准功能,可以在其他较新的计算机上运行。问题是 cUrl 会引发 Json 格式错误,尽管似乎没有任何特殊字符异常。

我尝试将引号更改为撇号,反之亦然,并使用反斜杠和插入符号作为转义字符。这个 wget 脚本有效:

wget --quiet ^
  --method POST ^
  --header 'content-type: application/json' ^
  --body-data '{"method":"passthrough", "params": {"deviceId": "[MyId]", "requestData": "{\"system\":{\"set_relay_state\":{\"state\":0}}}" }}' ^
  --output-document ^
  - 'https://eu-wap.tplinkcloud.com/?token=[MyToken]'

但是 cUrl 中完全相同的 JSON 被拒绝:

curl -X POST -H 'content-type:application/json' -d '{"method":"passthrough","params":{"deviceId":"[MyId]","requestData":"{\"system\":{\"set_relay_state\":{\"state\":0}}}"}}' https://wap.tplinkcloud.com?token=[MyToken]

感谢您的任何建议。

标签: jsonwindowscurlwget

解决方案


  • C:\Windows\System32\curl.exe在 Windows 的命令行上使用 Windows 10 中的 curl ( )。

如果我的理解是正确的,那么这个修改呢?

为 Windows 修改的 curl 命令:

curl -H "content-type:application/json" -d "{\"method\":\"passthrough\",\"params\":{\"deviceId\":\"[MyId]\",\"requestData\":\"{\\\"system\\\":{\\\"set_relay_state\\\":{\\\"state\\\":0}}}\"}}" https://eu-wap.tplinkcloud.com/?token=[MyToken]
  • 在 Windows 命令行中,请使用双引号而不是单引号。
  • 在 JSON 对象处,请转义每个双引号。

笔记:

  • 我不确定您回复中的正确 URL。因此,请在进行上述测试时使用正确的 URL。

推荐阅读