From 37da873da9777d852850d7d53a099fd637b7e3b2 Mon Sep 17 00:00:00 2001 From: Sven Fuchs Date: Thu, 14 Oct 2021 20:58:13 +0200 Subject: [PATCH] Fetch headers in POST example for native requests In the POST section of `http_native_request` headers are not fetched. thus, just commenting out the GET request example would result in a `status` and `content_length` of `0` being reported (quite confusing). (Plus, it should log `HTTP POST Status` here, instead of `HTTP GET Status`.) Merges https://github.com/espressif/esp-idf/pull/7696 --- .../main/esp_http_client_example.c | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/examples/protocols/esp_http_client/main/esp_http_client_example.c b/examples/protocols/esp_http_client/main/esp_http_client_example.c index 85d9405f37..26d97b29ae 100644 --- a/examples/protocols/esp_http_client/main/esp_http_client_example.c +++ b/examples/protocols/esp_http_client/main/esp_http_client_example.c @@ -624,14 +624,19 @@ static void http_native_request(void) if (wlen < 0) { ESP_LOGE(TAG, "Write failed"); } - int data_read = esp_http_client_read_response(client, output_buffer, MAX_HTTP_OUTPUT_BUFFER); - if (data_read >= 0) { - ESP_LOGI(TAG, "HTTP GET Status = %d, content_length = %d", - esp_http_client_get_status_code(client), - esp_http_client_get_content_length(client)); - ESP_LOG_BUFFER_HEX(TAG, output_buffer, strlen(output_buffer)); + content_length = esp_http_client_fetch_headers(client); + if (content_length < 0) { + ESP_LOGE(TAG, "HTTP client fetch headers failed"); } else { - ESP_LOGE(TAG, "Failed to read response"); + int data_read = esp_http_client_read_response(client, output_buffer, MAX_HTTP_OUTPUT_BUFFER); + if (data_read >= 0) { + ESP_LOGI(TAG, "HTTP POST Status = %d, content_length = %d", + esp_http_client_get_status_code(client), + esp_http_client_get_content_length(client)); + ESP_LOG_BUFFER_HEX(TAG, output_buffer, strlen(output_buffer)); + } else { + ESP_LOGE(TAG, "Failed to read response"); + } } } esp_http_client_cleanup(client);