From bbd57a220478c5b7f58e8eb435d2a531ec51f1dc Mon Sep 17 00:00:00 2001 From: Mathew Harman Date: Thu, 3 Apr 2025 15:57:04 -0400 Subject: [PATCH 1/2] fix(esp_http_client): fix error return when set_header has value==NULL Prior to v5.4, headers could be deleted from an HTTP client by calling esp_http_client_set_header("Header", NULL). This pattern is used by esp_http_client_set_post_field(NULL, 0), which is the only API usage that will delete the request body in a persistent connection scenario. --- components/esp_http_client/esp_http_client.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/esp_http_client/esp_http_client.c b/components/esp_http_client/esp_http_client.c index ef5130ec20..96fb635fcc 100644 --- a/components/esp_http_client/esp_http_client.c +++ b/components/esp_http_client/esp_http_client.c @@ -357,7 +357,7 @@ static int http_on_chunk_header(http_parser *parser) esp_err_t esp_http_client_set_header(esp_http_client_handle_t client, const char *key, const char *value) { - if (client == NULL || client->request == NULL || client->request->headers == NULL || key == NULL || value == NULL) { + if (client == NULL || client->request == NULL || client->request->headers == NULL || key == NULL) { return ESP_ERR_INVALID_ARG; } From 27951298abfaec584a6a4b974f13c7fa48aac669 Mon Sep 17 00:00:00 2001 From: "nilesh.kale" Date: Fri, 4 Apr 2025 15:54:53 +0530 Subject: [PATCH 2/2] fix(esp_http_client): Added test case to verify set header functionality This commit added testcase to verify esp_http_client_set_header allows header value as NULL. Setting this NULL will delete the header. Closes https://github.com/espressif/esp-idf/issues/15714 --- .../test_apps/main/test_http_client.c | 22 ++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/components/esp_http_client/test_apps/main/test_http_client.c b/components/esp_http_client/test_apps/main/test_http_client.c index f066c2d52b..309e948b7f 100644 --- a/components/esp_http_client/test_apps/main/test_http_client.c +++ b/components/esp_http_client/test_apps/main/test_http_client.c @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2018-2024 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2018-2025 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -166,6 +166,26 @@ TEST_CASE("esp_http_client_get_url() should return URL in the correct format", " TEST_ASSERT_EQUAL_STRING(url, client_url); } +TEST_CASE("esp_http_client_set_header() should not return error if header value is NULL", "[esp_http_client]") +{ + esp_http_client_config_t config = { + .url = "http://httpbin.org:8080/post", + }; + + esp_http_client_handle_t client = esp_http_client_init(&config); + TEST_ASSERT_NOT_NULL(client); + + // First, set a valid header + esp_err_t err = esp_http_client_set_header(client, "Test-Header", "dummy_value"); + TEST_ASSERT_EQUAL(ESP_OK, err); + + // Now, delete the header by passing value = NULL + err = esp_http_client_set_header(client, "Test-Header", NULL); + TEST_ASSERT_EQUAL(ESP_OK, err); // Ensure it does NOT return ESP_ERR_INVALID_ARG + + esp_http_client_cleanup(client); +} + void app_main(void) { unity_run_menu();