首页 > 解决方案 > Windows 10 上的 curl:“标题中的无效字符”

问题描述

我正在尝试在REST API Home Assistant Developer Docs的 Windows 10 上使用下面的 curl 示例。

curl -X POST -H "Authorization: Bearer ABCDEFGH" \
       -H "Content-Type: application/json" \
       -d '{"entity_id": "switch.christmas_lights"}' \
       http://localhost:8123/api/services/switch/turn_on

下面是我的实际命令行(转义 Windows 的双引号):

curl -X POST -H "Authorization: Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiI5OTc5ZDUwZjA2MDk0YmQ1YTBkYjkyYmQ5MjEyYWZkNyIsImlhdCI6MTU4Nzc5MzI4NywiZXhwIjoxOTAzMTUzMjg3fQ.8sjGRXL1spXguuRnp795049zdCFcOtraCiDjC61UkWQ" -H "{\"Content-Type: application/json\"}" -d "{\"entity_id\": \"light.media_room_light\"}" http://homeassistant.local:8123/api/services/switch/turn_on

它以以下错误消息响应。我正在使用从中下载的 curl.exe

invalid character in header

我从以下网址下载curl.exe https ://curl.haxx.se/windows/

标签: windowscurl

解决方案


我认为您的 MIME Con​​tent-Type 标头与 json 编码数据字段混合在一起。

而不是这个:

-H "{\"Content-Type: application/json\"}"

这个:

-H "Content-Type: application/json"

扁平化为一行:

curl -X POST -H "Authorization: Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiI5OTc5ZDUwZjA2MDk0YmQ1YTBkYjkyYmQ5MjEyYWZkNyIsImlhdCI6MTU4Nzc5MzI4NywiZXhwIjoxOTAzMTUzMjg3fQ.8sjGRXL1spXguuRnp795049zdCFcOtraCiDjC61UkWQ" -H "Content-Type: application/json" -d "{\"entity_id\": \"light.media_room_light\"}" http://homeassistant.local:8123/api/services/switch/turn_on

推荐阅读