Page 1 of 1

HTTP client fails to retreive response data after esp_http_client_perform

Posted: Thu Nov 28, 2024 4:42 pm
by igormoo
I've tried a lot of different ways and examples. I'm sure I'm missing something obvious but I already spent a couple of full days on this. Any ideas are welcome

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.
  1. std::string OtaUpdater::http_post(const std::string &url, const std::string &payload) {
  2.   esp_http_client_config_t config_post = {
  3.     .url = url.c_str(),
  4.     .method = HTTP_METHOD_POST,
  5.     .timeout_ms = 60000,
  6.     // .event_handler = client_event_post_handler,
  7.     // .buffer_size = OTA_BUF_SIZE,
  8.     // .buffer_size_tx = 1024,
  9.   };
  10.  
  11.   esp_http_client_handle_t client = esp_http_client_init(&config_post);
  12.  
  13.   char  *post_data = "{\"User\":\"User1\"}";
  14.   esp_http_client_set_post_field(client, post_data, strlen(post_data));
  15.   esp_http_client_set_header(client, "Content-Type", "application/json");
  16.  
  17.   esp_http_client_perform(client);
  18.  
  19.   int content_length = esp_http_client_get_content_length(client);
  20.   // int content_length = esp_http_client_fetch_headers(client);
  21.   ESP_LOGI(TAG, "Content length: %d", content_length);
  22.  
  23.   if (content_length > 0) {
  24.     // char *buffer = (char*)malloc(content_length + 1);
  25.     char buffer[MAX_HTTP_OUTPUT_BUFFER];
  26.     int read_len = esp_http_client_read(client, buffer, MAX_HTTP_OUTPUT_BUFFER);
  27.     // int read_len = esp_http_client_read_response(client, buffer, MAX_HTTP_OUTPUT_BUFFER);
  28.     if (read_len > 0) {
  29.       buffer[read_len] = '\0';
  30.       printf("Response: %s\n", buffer);
  31.     }
  32.     else {
  33.       ESP_LOGE(TAG, "received 0 read");
  34.     }
  35.   }
  36.   else {
  37.     ESP_LOGE(TAG, "Error reading HTTP data");
  38.     esp_http_client_cleanup(client);
  39.     return "";
  40.   }
  41.  
  42.   esp_http_client_cleanup(client);
  43.  
  44.   ESP_LOGI(TAG, "Sent data");
  45.  
  46.   return"";
  47. }
Screenshot 2024-11-28 at 11.37.56 AM.png
Screenshot 2024-11-28 at 11.37.56 AM.png (780.6 KiB) Viewed 551 times