【发布时间】:2019-05-01 18:04:50
【问题描述】:
我正在尝试找到使用 Alamofire 从 Swift 4 调用 ecobee 的 API 的正确语法。
他们的 cURL 示例:
curl -H "Content-Type: text/json" -H "Authorization: Bearer ACCESS_TOKEN" 'https://api.ecobee.com/1/thermostat?format=json&body=\{"selection":\{"selectionType":"registered","selectionMatch":"","includeRuntime":true\}\}'
我最接近解决方案的是这个
func doRequest() {
guard let url = URL(string: "https://api.ecobee.com/1/thermostat?format=json") else { return }
let parameters: Parameters = [
"selection": [
"selectionType": "registered",
"selectionMatch": ""
]
]
let headers: HTTPHeaders = [
"Content-Type": "text/json",
"Authorization": "Bearer \(core.accessToken)"
]
let req = AF.request(url, method: .get, parameters: parameters, encoding: JSONEncoding.default, headers: headers)
.responseJSON { response in
print("Error:", response.error?.localizedDescription ?? "no error")
print("Data:", String(data: response.data!, encoding: .utf8)!)
}
debugPrint(req)
}
当我运行它时,调用最终失败,状态码为 408,服务器超时。
当我将 HTTP 方法更改为使用 .post 时,调用完成,但响应为内部状态 3,并显示消息“由于通信错误,更新失败。”
在我浪费一天时间试图破解它之前,谁能帮我弄清楚我做错了什么?
【问题讨论】:
-
我不确定您在看什么文档,但更新恒温器应该是一个 POST:ecobee.com/home/developer/api/documentation/v1/operations/…
-
啊,我明白了,获取所有恒温器需要一个带有奇怪参数编码的 GET。
标签: swift alamofire ecobee-api