#include "http_client.h" #include "esp_http_client.h" #include "esp_tls.h" #include "esp_log.h" #include #include #define MAX_HTTP_RECV_BUFFER 512 #define MAX_HTTP_OUTPUT_BUFFER 2048 static const char *TAG = "http_client"; esp_err_t _http_event_handler(esp_http_client_event_t *evt) { static char *output_buffer; // Buffer to store response of http request from event handler static int output_len; // Stores number of bytes read switch (evt->event_id) { case HTTP_EVENT_ERROR: ESP_LOGD(TAG, "HTTP_EVENT_ERROR"); break; case HTTP_EVENT_ON_CONNECTED: ESP_LOGD(TAG, "HTTP_EVENT_ON_CONNECTED"); break; case HTTP_EVENT_HEADER_SENT: ESP_LOGD(TAG, "HTTP_EVENT_HEADER_SENT"); break; case HTTP_EVENT_ON_HEADER: ESP_LOGD(TAG, "HTTP_EVENT_ON_HEADER, key=%s, value=%s", evt->header_key, evt->header_value); break; case HTTP_EVENT_ON_DATA: ESP_LOGD(TAG, "HTTP_EVENT_ON_DATA, len=%d", evt->data_len); // Clean the buffer in case of a new request if (output_len == 0 && evt->user_data) { // we are just starting to copy the output data into the use memset(evt->user_data, 0, MAX_HTTP_OUTPUT_BUFFER); } /* * Check for chunked encoding is added as the URL for chunked encoding used in this example returns binary data. * However, event handler can also be used in case chunked encoding is used. */ if (!esp_http_client_is_chunked_response(evt->client)) { // If user_data buffer is configured, copy the response into the buffer int copy_len = 0; if (evt->user_data) { // The last byte in evt->user_data is kept for the NULL character in case of out-of-bound access. copy_len = MIN(evt->data_len, (MAX_HTTP_OUTPUT_BUFFER - output_len)); if (copy_len) { memcpy(evt->user_data + output_len, evt->data, copy_len); } } else { int content_len = esp_http_client_get_content_length(evt->client); if (output_buffer == NULL) { // We initialize output_buffer with 0 because it is used by strlen() and similar functions therefore should be null terminated. output_buffer = (char *)calloc(content_len + 1, sizeof(char)); output_len = 0; if (output_buffer == NULL) { ESP_LOGE(TAG, "Failed to allocate memory for output buffer"); return ESP_FAIL; } } copy_len = MIN(evt->data_len, (content_len - output_len)); if (copy_len) { memcpy(output_buffer + output_len, evt->data, copy_len); } } output_len += copy_len; } break; case HTTP_EVENT_ON_FINISH: ESP_LOGD(TAG, "HTTP_EVENT_ON_FINISH"); if (output_buffer != NULL) { // Print buffer ESP_LOG_BUFFER_HEX(TAG, output_buffer, output_len); free(output_buffer); output_buffer = NULL; } output_len = 0; break; case HTTP_EVENT_DISCONNECTED: ESP_LOGI(TAG, "HTTP_EVENT_DISCONNECTED"); int mbedtls_err = 0; esp_err_t err = esp_tls_get_and_clear_last_error((esp_tls_error_handle_t)evt->data, &mbedtls_err, NULL); if (err != 0) { ESP_LOGI(TAG, "Last esp error code: 0x%x", err); ESP_LOGI(TAG, "Last mbedtls failure: 0x%x", mbedtls_err); } if (output_buffer != NULL) { free(output_buffer); output_buffer = NULL; } output_len = 0; break; case HTTP_EVENT_REDIRECT: ESP_LOGD(TAG, "HTTP_EVENT_REDIRECT"); esp_http_client_set_header(evt->client, "From", "user@example.com"); esp_http_client_set_header(evt->client, "Accept", "text/html"); esp_http_client_set_redirection(evt->client); break; } return ESP_OK; } char *http_client_exec(const http_request_opts *opts) { char local_response_buffer[MAX_HTTP_OUTPUT_BUFFER + 1] = {0}; ESP_LOGI(TAG, "Perform HTTP request on %s", opts->url); esp_http_client_config_t config = { .event_handler = _http_event_handler, .user_data = local_response_buffer, .url = opts->url, .disable_auto_redirect = true, }; esp_http_client_handle_t client = esp_http_client_init(&config); esp_err_t err = esp_http_client_perform(client); const int status = esp_http_client_get_status_code(client); const int64_t resp_len = esp_http_client_get_content_length(client); esp_http_client_cleanup(client); if (err != ESP_OK) { ESP_LOGE(TAG, "HTTP request failed with code %xd!", err); return NULL; } if (status < 200 || status > 299) { ESP_LOGE(TAG, "HTTP request failed with status %d!", status); return NULL; } local_response_buffer[resp_len] = 0; return strdup(local_response_buffer); }