Json ESP32-http请求后响应无效

Json ESP32-http请求后响应无效,json,https,esp32,Json,Https,Esp32,我试图做一个post请求,然后读出响应来处理这个过程 我得到的答复如下: {“版本”:“0.0.7”} 或者这就是我在《邮差》里看到的 //检查ESP it的版本号是否需要更新 WiFiClientSecure checkForUpdateClient; Serial.println(F(“检查更新!”); checkForUpdateClient.setUnsecure(); 如果(!checkForUpdateClient.connect(数字\u HQ\u BASE\u URL,443))

我试图做一个post请求,然后读出响应来处理这个过程

我得到的答复如下:
{“版本”:“0.0.7”}

或者这就是我在《邮差》里看到的

//检查ESP it的版本号是否需要更新
WiFiClientSecure checkForUpdateClient;
Serial.println(F(“检查更新!”);
checkForUpdateClient.setUnsecure();
如果(!checkForUpdateClient.connect(数字\u HQ\u BASE\u URL,443)){
Serial.println(F(“连接失败”);
返回;
}
Serial.println(F(“Connected!”);
静态文件;
doc[“key”]=设备机密;
字符串更新数据;
序列化JSON(文档,更新数据);
//发出HTTP请求:
checkForUpdateClient.print(“POST”);//注意太空!
checkForUpdateClient.print(数字版\u HQ\u端点);//API端点
checkForUpdateClient.println(“HTTP/1.1”);//注意太空!
checkForUpdateClient.print(“主机:”);
checkForUpdateClient.println(数字\u HQ\u BASE\u URL);
checkForUpdateClient.println(“连接:关闭”);
checkForUpdateClient.print(“用户代理:”);
checkForUpdateClient.println(数字用户代理);
checkForUpdateClient.println(“内容类型:application/json”);
checkForUpdateClient.print(“内容长度:”);
checkForUpdateClient.println(updateData.length());
checkForUpdateClient.println();
checkForUpdateClient.println(updateData);
checkForUpdateClient.println();
如果(checkForUpdateClient.println()==0){
Serial.println(F(“发送请求失败”);
checkForUpdateClient.stop();
返回;
}
//检查HTTP状态
字符状态[32]={0};
checkForUpdateClient.readBytesUntil('\r',status,sizeof(status));
Serial.println(“状态:”);
Serial.println(状态);
如果(strcmp(状态为“HTTP/1.1200正常”)!=0){
连续打印(F(“意外响应:”);
//状态可用于筛选出响应代码
Serial.println(状态);
checkForUpdateClient.stop();
返回;
}
//跳过HTTP头
char endOfHeaders[]=“\r\n\r\n”;
如果(!checkForUpdateClient.find(endOfHeaders)){
Serial.println(F(“无效响应”);
checkForUpdateClient.stop();
返回;
}
print(“构建容量为的JSON对象”);
const size\u t objCapacity=JSON\u OBJECT\u size(1)+60;//开销
动态响应(objCapacity);
//解析JSON对象
反序列化错误=反序列化JSON(OTA响应,checkForUpdateClient)
如果(错误){
print(F(“反序列化JSON()失败:”);
Serial.println(error.f_str());
checkForUpdateClient.stop();
返回;
}
//提取值
序列号println(F)(“回复:”);
Serial.println(otaResponse.as());
Serial.println(otaResponse[“version”].as());
如果(OTA响应[“版本”].as()>数字\u HQ\u软件\u版本){
//更新
Serial.println(“系统需要更新”);
doUpdate();//触发更新序列
}否则{
Serial.println(“设备是最新的”);
}
问题是我有以下的回答(序列)

现在我只需要json对响应进行解码,然后我需要能够检查它的值

问题是,
checkForUpdateClient
返回的是“1”,而不是对象。虽然我仍然有与此url完全相同的代码:
api.ipify.org/?format=json
,但在那里它工作得很好


我做错了什么?

所以问题是我没有在服务器API中发送内容长度头。一旦我添加了这些,我就可以使用我写的代码了,哈哈。。。感谢大家成为我的橡皮鸭:s

如果我要调试它,我要做的第一件事就是通过打印POST请求返回的内容来确定“invalidInput”是什么。当您反序列化JSON时,您得到了一个错误;为什么您希望
checkForUpdateClient
返回一个对象?我不知道这是如何工作的。。。我有一段模拟代码,它在这里工作:这段代码返回一个IP地址。我不知道这段代码有什么不同……好吧,你的第一个任务就是按照romkey说的做。在将服务器提供给JSON解析器之前,打印出从服务器获得的内容。下面是如何完成的一个示例-参见第92行的最后一个循环-95@Tarmo所以我这样做了,然后连接就失败了。现在,我已经看到我得到了正确的响应,但它有一个奇怪的前缀和一个奇怪的后缀(行),就像这样:我不知道为什么会这样…这是你的服务器发送的。无法对此发表评论,除非我不希望HTTP响应中出现终止零。如果没有内容长度,服务器将响应正文作为HTTP分块传输编码发送,因此它不是有效的JSON
17:11:38.366 -> Checking for updates!
17:11:39.152 -> Connected!
17:11:39.841 -> Status:
17:11:39.874 -> HTTP/1.1 200 OK
17:11:39.874 -> Building the JSON object with a capacity of deserializeJson() failed: InvalidInput