diff --git a/components/esp_http_client/esp_http_client.c b/components/esp_http_client/esp_http_client.c index db63ca0ed2..650b65ed34 100644 --- a/components/esp_http_client/esp_http_client.c +++ b/components/esp_http_client/esp_http_client.c @@ -1377,6 +1377,26 @@ int esp_http_client_read_response(esp_http_client_handle_t client, char *buffer, return read_len; } +esp_err_t esp_http_client_flush_response(esp_http_client_handle_t client, int *len) +{ + if (client == NULL) { + ESP_LOGE(TAG, "client must not be NULL"); + return ESP_ERR_INVALID_ARG; + } + int read_len = 0; + while (!esp_http_client_is_complete_data_received(client)) { + int data_read = esp_http_client_get_data(client); + if (data_read < 0) { + return ESP_FAIL; + } + read_len += data_read; + } + if (len) { + *len = read_len; + } + return ESP_OK; +} + esp_err_t esp_http_client_get_url(esp_http_client_handle_t client, char *url, const int len) { if (client == NULL || url == NULL) { diff --git a/components/esp_http_client/include/esp_http_client.h b/components/esp_http_client/include/esp_http_client.h index 3ad04b49d8..a942426afa 100644 --- a/components/esp_http_client/include/esp_http_client.h +++ b/components/esp_http_client/include/esp_http_client.h @@ -524,6 +524,21 @@ bool esp_http_client_is_complete_data_received(esp_http_client_handle_t client); int esp_http_client_read_response(esp_http_client_handle_t client, char *buffer, int len); +/** + * @brief Process all remaining response data + * This uses an internal buffer to repeatedly receive, parse, and discard response data until complete data is processed. + * As no additional user-supplied buffer is required, this may be preferrable to `esp_http_client_read_response` in situations where the content of the response may be ignored. + * + * @param[in] client The esp_http_client handle + * @param len Length of data discarded + * + * @return + * - ESP_OK If successful, len will have discarded length + * - ESP_FAIL If failed to read response + * - ESP_ERR_INVALID_ARG If the client is NULL + */ +int esp_http_client_flush_response(esp_http_client_handle_t client, int *len); + /** * @brief Get URL from client *