ESP32 post empty body using ESPIDF

23 Views Asked by At

im trying to do a post request but for some reason the Content-Length is correct and the post data is empty. When I try request on postman for example it works normally. In both case the endpoint sends the response normally too, the only problem is really the empty body.

that is the server request print requested by the Postman:

POST /api/testpost HTTP/1.1 Content-Type: text/plain User-Agent: PostmanRuntime/7.32.1 Cache-Control: no-cache Postman-Token: 5038e2b6-8734-4fd2-95c0-898d2c15610f Host: 192.168.1.5:8888 Content-Length: 10

{"data":"2"}

that is the server request print requested by the ESP32:

POST /api/testpost HTTP/1.1 User-Agent: ESP32 HTTP Client/1.0 Host: 192.168.1.5:8888 Content-Type: text/plain Content-Length: 10

the code Im using:

void perform_post_request(const char *url) {

    esp_http_client_config_t config_post = {
        .url = url,
        .method = HTTP_METHOD_POST,
        .cert_pem = NULL,
        .event_handler = client_event_post_handler
    };
        
    esp_http_client_handle_t client = esp_http_client_init(&config_post);

    const char *post_data = "{\"data\":2}";
    size_t content_length = strlen(post_data);

    char content_length_str[16]; 
    snprintf(content_length_str, sizeof(content_length_str), "%d", content_length);

    esp_http_client_set_header(client, "Content-Type", "text/plain");

    esp_http_client_set_header(client, "Content-Length", content_length_str);

    printf("%s\n",post_data);

    esp_http_client_set_post_field(client, post_data, content_length);


    esp_err_t err = esp_http_client_perform(client);
    if (err == ESP_OK) {
        ESP_LOGI(TAG, "POST request sent successfully");
    } else {
        ESP_LOGE(TAG, "Failed to send POST request, error code: %d", err);
    }

    esp_http_client_cleanup(client);

}

I really cant identify the problem but I think is some config on esp32

0

There are 0 best solutions below