Page 1 of 1

HTTP Post closes connection before the payload is sent

Posted: Thu Nov 28, 2024 1:03 am
by igormoo
I've spent a lot of time fighting with this simple code. Most likely I'm missing some setting but I can't find it in the documentation and hundreds of samples I've gone through.

What can be the reason that the POST payload is not sent over the wire? I've used WireShark to do the network trace - no payload is sent, but I see the headers correctly and the data is properly received back from the server.

I'm using ESPIDF v5.1.5 - I can't update to the newer one due to heave backward compatibility limitations
  1. esp_err_t client_event_post_handler(esp_http_client_event_handle_t evt)
  2. {
  3.     switch (evt->event_id)
  4.     {
  5.     case HTTP_EVENT_ON_DATA:
  6.         ESP_LOGI("HANDLER", "HTTP_EVENT_ON_DATA: %.*s\n", evt->data_len, (char *)evt->data);
  7.         break;
  8.  
  9.     default:
  10.         break;
  11.     }
  12.     return ESP_OK;
  13. }
  14.  
  15. std::string http_post(const std::string &url, const std::string &payload) {
  16.   esp_http_client_config_t config_post = {
  17.       .url = url.c_str(),
  18.       .cert_pem = NULL,
  19.       .method = HTTP_METHOD_POST,
  20.       .event_handler = client_event_post_handler};
  21.      
  22.   esp_http_client_handle_t client = esp_http_client_init(&config_post);
  23.  
  24.   char  *post_data = "{\"User\":\"User1\"}";
  25.   esp_http_client_set_post_field(client, post_data, strlen(post_data));
  26.   esp_http_client_set_header(client, "Content-Type", "application/json");
  27.  
  28.   esp_http_client_perform(client);
  29.   esp_http_client_cleanup(client);
  30.  
  31.   ESP_LOGI(TAG, "Sent data");
  32.  
  33.   return "";
  34. }

Re: HTTP Post closes connection before the payload is sent

Posted: Thu Nov 28, 2024 3:59 am
by boarchuz
Is it possible that there's an issue on the server side?

If you send the same request from your PC, what behaviour do you get?

Check for any errors (especially returned from _perform).

Re: HTTP Post closes connection before the payload is sent

Posted: Thu Nov 28, 2024 4:53 pm
by igormoo
Thank you for the idea. Actually, the server was tested with "curl", but when I've checked via WireShare I've noticed that the server was rushing with the response to the first chunk that was sent by the espidf. I've updated the server to wait for the whole payload.

So the question remains - is there a way to tell ESPIDF not to send the data in parts but to send only when the whole payload has been fully written by the client?

Re: HTTP Post closes connection before the payload is sent

Posted: Thu Nov 28, 2024 11:30 pm
by boarchuz
If I understand correctly, the server was starting its response after the request headers instead of waiting for the complete request, but that's now fixed?

So I'm not sure what the question means. The ESP32 is the client. It's the HTTP server's responsibility to wait for the whole payload to be sent.