我正在编写一个bash脚本来与我的ecobee接口(查询信息和更改设置)。我已经完成了所有的授权(访问令牌和刷新令牌),现在正在尝试从ecobee请求信息。这个json参数列表是动态创建的。开发人员API中的卷曲示例似乎都不起作用。
我已经尝试将json分配给一个变量(?json="$selection")和一个文件(?json=@"$location")。我最近的一次尝试(硬编码json和转义大括号)如下所示:
response=$(curl -s "https://api.ecobee.com/1/thermostat?json=\{"selection":\{"selectionType":"registered","selectionMatch":null,"includeRuntime":true,"includeSettings":true/}/}" -H "Content-Type: application/json;charset=UTF-8" -H "Authorization: Bearer $access_token")我收到了一个空响应declare -- response=""
如果我从文件中读取卷曲:{"selection":{"selectionType":"registered","selectionMatch":null,"includeRuntime":true,"includeSettings":true}}
然后我收到:
response='{
"status": {
"code": 4,
"message": "Serialization error. Malformed json. Check your request and parameters are valid."
}
}'我认为这是逃亡的问题?
有人能提供任何洞察力吗?再说一次,我想这是最简单的部分。我正在使用jq构建我的请求json。其他可以更好地对付json的卷发的方法?我被限制在bash (据我所知)
谢谢
发布于 2022-02-15 07:32:30
要将(任意)数据集成到一个URL (或一般的URI )中,您需要首先使用百分比编码来准备它。
正如您已经提到的使用jq来组合数据一样,您可以将@uri添加到jq过滤器中以执行编码,并使用--raw-output (或-r)选项正确地输出它。
例如,jq -r '@uri'应用于您的JSON
{
"selection": {
"selectionType": "registered",
"selectionMatch": null,
"includeRuntime": true,
"includeSettings": true
}
}输出会吗
%7B%22selection%22%3A%7B%22selectionType%22%3A%22registered%22%2C%22selectionMatch%22%3Anull%2C%22includeRuntime%22%3Atrue%2C%22includeSettings%22%3Atrue%7D%7D可以在查询字符串 ?json=…中使用。
https://stackoverflow.com/questions/71114496
复制相似问题