feat(esp_http_client): adds support to save response headers

Closes https://github.com/espressif/esp-idf/issues/17695
This commit is contained in:
Ashish Sharma
2026-02-03 14:40:08 +08:00
parent 148e333495
commit c8f1fa86a2
9 changed files with 189 additions and 4 deletions
@@ -440,6 +440,28 @@ static void https_with_url(void)
ESP_LOGI(TAG, "HTTPS Status = %d, content_length = %"PRId64,
esp_http_client_get_status_code(client),
esp_http_client_get_content_length(client));
#if CONFIG_ESP_HTTP_CLIENT_SAVE_RESPONSE_HEADERS
ESP_LOGI(TAG, "Response headers: ");
char *header_value = NULL;
esp_err_t err = esp_http_client_get_response_header(client, "Content-Length", &header_value);
if (err == ESP_OK) {
ESP_LOGI(TAG, "Content-Length: %s", header_value);
} else {
ESP_LOGE(TAG, "Error getting Content-Length header: %s", esp_err_to_name(err));
}
err = esp_http_client_get_response_header(client, "Date", &header_value);
if (err == ESP_OK) {
ESP_LOGI(TAG, "Date: %s", header_value);
} else {
ESP_LOGE(TAG, "Error getting Date header: %s", esp_err_to_name(err));
}
err = esp_http_client_get_response_header(client, "Server", &header_value);
if (err == ESP_OK) {
ESP_LOGI(TAG, "Server: %s", header_value);
} else {
ESP_LOGE(TAG, "Error getting Server header: %s", esp_err_to_name(err));
}
#endif // CONFIG_ESP_HTTP_CLIENT_SAVE_RESPONSE_HEADERS
} else {
ESP_LOGE(TAG, "Error perform http request %s", esp_err_to_name(err));
}
@@ -41,6 +41,9 @@ def test_examples_protocol_esp_http_client(dut: Dut) -> None:
dut.expect(r'HTTP Absolute path redirect Status = 200, content_length = (\d)')
dut.expect(r'HTTP Absolute path redirect \(manual\) Status = 200, content_length = (\d)')
dut.expect(r'HTTPS Status = 200, content_length = (\d)')
dut.expect(r'Content-Length: (\d+)')
dut.expect(r'Date: (\w+)')
dut.expect(r'Error getting Server header: ESP_ERR_NOT_FOUND')
dut.expect(r'HTTPS Status = 200, content_length = (\d)')
dut.expect(r'HTTP redirect to HTTPS Status = 200, content_length = (\d)')
dut.expect(r'HTTP chunk encoding Status = 200, content_length = (-?\d)')
@@ -11,3 +11,5 @@ CONFIG_ESP_HTTP_CLIENT_ENABLE_BASIC_AUTH=y
CONFIG_ESP_HTTP_CLIENT_ENABLE_DIGEST_AUTH=y
CONFIG_EXAMPLE_HTTP_ENDPOINT="httpbin.espressif.cn"
CONFIG_MBEDTLS_CERTIFICATE_BUNDLE_CROSS_SIGNED_VERIFY=y
CONFIG_MBEDTLS_SSL_PROTO_TLS1_3=y
CONFIG_ESP_HTTP_CLIENT_SAVE_RESPONSE_HEADERS=y