ESP32 - http 请求后响应无效

ESP32 - response not valid after http request

我正在尝试执行 post 请求,然后读出响应以处理此请求。

我得到的回复如下: {"version":"0.0.7"}

或者这就是我在 Postman 中的内容。

// Check if the ESP it's version number is in need of an update
WiFiClientSecure checkForUpdateClient;
Serial.println(F("Checking for updates!"));
checkForUpdateClient.setInsecure();
if (!checkForUpdateClient.connect(DIGITAL_HQ_BASE_URL, 443)) {
    Serial.println(F("Connection failed"));
    return;
}
Serial.println(F("Connected!"));
StaticJsonDocument<64> doc;
doc["key"] = DEVICE_SECRET;
String updateData;
serializeJson(doc, updateData);
// Make a HTTP request:
checkForUpdateClient.print("POST "); // watch the space!
checkForUpdateClient.print(DIGITAL_HQ_VERSION_ENDPOINT); // API endpoint
checkForUpdateClient.println(" HTTP/1.1"); // watch the space!
checkForUpdateClient.print("Host: ");
checkForUpdateClient.println(DIGITAL_HQ_BASE_URL);
checkForUpdateClient.println("Connection: close");
checkForUpdateClient.print("User-Agent: ");
checkForUpdateClient.println(DIGITAL_HQ_USER_AGENT);
checkForUpdateClient.println("Content-Type: application/json");
checkForUpdateClient.print("Content-Length: ");
checkForUpdateClient.println(updateData.length());
checkForUpdateClient.println();
checkForUpdateClient.println(updateData);
checkForUpdateClient.println();
if (checkForUpdateClient.println() == 0) {
    Serial.println(F("Failed to send request"));
    checkForUpdateClient.stop();
    return;
}
// Check HTTP status
char status[32] = {0};
checkForUpdateClient.readBytesUntil('\r', status, sizeof(status));
Serial.println("Status:");
Serial.println(status);
if (strcmp(status, "HTTP/1.1 200 OK") != 0) {
    Serial.print(F("Unexpected response: "));
    // Status can be used to filter out the response code
    Serial.println(status);
    checkForUpdateClient.stop();
    return;
}
// Skip HTTP headers
char endOfHeaders[] = "\r\n\r\n";
if (!checkForUpdateClient.find(endOfHeaders)) {
    Serial.println(F("Invalid response"));
    checkForUpdateClient.stop();
    return;
}
Serial.print("Building the JSON object with a capacity of ");
const size_t objCapacity = JSON_OBJECT_SIZE(1) + 60; // overhead
DynamicJsonDocument otaResponse(objCapacity);
// Parse JSON object
DeserializationError error = deserializeJson(otaResponse, checkForUpdateClient)
if (error) {
    Serial.print(F("deserializeJson() failed: "));
    Serial.println(error.f_str());
    checkForUpdateClient.stop();
    return;
}
// Extract values
Serial.println(F("Response:"));
Serial.println(otaResponse.as<String>());
Serial.println(otaResponse["version"].as<String>());
if (otaResponse["version"].as<String>() > DIGITAL_HQ_SOFTWARE_VERSION) {
    // UPDATE
    Serial.println("System required update");
    doUpdate(); // Triggers the update sequence
} else {
    Serial.println("Device is up to date");
}

问题是我有以下响应(串行);

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

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

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

我做错了什么?

所以问题是我没有在服务器 API 中发送 content-length header。一旦我添加了它,我就可以使用我写的代码了哈哈...感谢所有人成为我的橡皮鸭:s10=]