Here are a couple of interesting points:
1. See attached WireShark capture - the payload response from the server is coming correctly in a single package
2. I see in the verbose trace that the data properly arrives at the client
3. esp_http_client_fetch_headers returns -1 but esp_http_client_get_content_length returns the correct number that came with the header
4. Both esp_http_client_read and esp_http_client_read_response return 0
5. If I attach the event handler - the data is properly received by the HTTP_EVENT_ON_DATA event, while still nothing from the esp_http_client_read
6. Defining buffer_size in the esp_http_client_config_t has no effect
Using ESPIDF v5.1.5 due to the project's backward compatibility requirements
The server is implemented on NodeJS v19 with Express library and it has properly been tested with `curl` and Wireshark
This is somewhat related to another question - https://esp32.com/viewtopic.php?f=13&t=43247
Note: the code below is a simplified version of the original - I just removed all the error checks and other overhead to make it easier to read. It has the same behavior as the full code - I've tested.
- std::string OtaUpdater::http_post(const std::string &url, const std::string &payload) {
- esp_http_client_config_t config_post = {
- .url = url.c_str(),
- .method = HTTP_METHOD_POST,
- .timeout_ms = 60000,
- // .event_handler = client_event_post_handler,
- // .buffer_size = OTA_BUF_SIZE,
- // .buffer_size_tx = 1024,
- };
- esp_http_client_handle_t client = esp_http_client_init(&config_post);
- char *post_data = "{\"User\":\"User1\"}";
- esp_http_client_set_post_field(client, post_data, strlen(post_data));
- esp_http_client_set_header(client, "Content-Type", "application/json");
- esp_http_client_perform(client);
- int content_length = esp_http_client_get_content_length(client);
- // int content_length = esp_http_client_fetch_headers(client);
- ESP_LOGI(TAG, "Content length: %d", content_length);
- if (content_length > 0) {
- // char *buffer = (char*)malloc(content_length + 1);
- char buffer[MAX_HTTP_OUTPUT_BUFFER];
- int read_len = esp_http_client_read(client, buffer, MAX_HTTP_OUTPUT_BUFFER);
- // int read_len = esp_http_client_read_response(client, buffer, MAX_HTTP_OUTPUT_BUFFER);
- if (read_len > 0) {
- buffer[read_len] = '\0';
- printf("Response: %s\n", buffer);
- }
- else {
- ESP_LOGE(TAG, "received 0 read");
- }
- }
- else {
- ESP_LOGE(TAG, "Error reading HTTP data");
- esp_http_client_cleanup(client);
- return "";
- }
- esp_http_client_cleanup(client);
- ESP_LOGI(TAG, "Sent data");
- return"";
- }