即使是我在garmin网站上发现的这个例子,也存在着同样的问题。
https://developer.garmin.com/connect-iq/core-topics/https/
import Toybox.System;
import Toybox.Communications;
import Toybox.Lang;
class JsonTransaction {
// set up the response callback function
function onReceive(responseCode as Number, data as Dictionary?) as Void {
if (responseCode == 200) {
System.println("Request Successful"); // print success
} else {
System.println("Response: " + responseCode); // print response code
}
}
function makeRequest() as Void {
var url = "https://www.garmin.com"; // set the url
var params = { // set the parameters
"definedParams" => "123456789abcdefg"
};
var options = { // set the options
:method => Communications.HTTP_REQUEST_METHOD_GET, // set HTTP method
:headers => { // set headers
"Content-Type" => Communications.REQUEST_CONTENT_TYPE_URL_ENCODED},
// set response type
:responseType => Communications.HTTP_RESPONSE_CONTENT_TYPE_URL_ENCODED
};
var responseCallback = method(:onReceive); // set responseCallback to
// onReceive() method
// Make the Communications.makeWebRequest() call
Communications.makeWebRequest(url, params, options, method(:onReceive));
}
}有人能告诉我我做错了什么吗?
发布于 2022-11-14 16:51:29
返回代码-400意味着“请求类型的响应体数据无效”。根据SDK规范。
您正在请求响应类型的Communications.HTTP_RESPONSE_CONTENT_TYPE_URL_ENCODED,但在您的问题中,您表示您希望返回HTML,这几乎肯定不能被解析为URL编码的表单参数。
SDK似乎不支持HTML响应类型。即使您省略了预期的响应类型,服务器可能仍然会发送"application/html“,而SDK声明”如果响应中的内容类型头不是已知的HTTP_RESPONSE_CONTENT_TYPE_*类型之一,就会发生错误“,所以我想您是不走运的。
也许您可以尝试请求HTTP_RESPONSE_CONTENT_TYPE_TEXT_PLAIN以使服务器返回文本而不是HTML,然后您可以以某种方式使用它吗?
https://stackoverflow.com/questions/73705491
复制相似问题