esp_http_client_read() returning wrong data
Posted: Wed Jun 12, 2019 11:57 pm
Hi all,
I've been playing around with the esp_http_client_example, specifically the https_with_url() function. My goal is it to download a JSON file, however I've noticed some strange behaviour wenn trying to read the date from esp_http_client_read(). For small files (~100bytes) this seems to work fine, but as soon as I try to retrieve larger char arrays the output is scrambled.
For example this file will output:
Which is only the last portion of the file and also duplicated at the end. I'm not sure if the mistake is on my side in the way I retrieve the data from the function or if it is a bug.
Here is the code I used:
Thank you for your time!
I've been playing around with the esp_http_client_example, specifically the https_with_url() function. My goal is it to download a JSON file, however I've noticed some strange behaviour wenn trying to read the date from esp_http_client_read(). For small files (~100bytes) this seems to work fine, but as soon as I try to retrieve larger char arrays the output is scrambled.
For example this file will output:
Code: Select all
Armada (@martuishere) and Javier Usobiaga (@htmlboy)
/* SITE */
Last update:2012/02/04
Language: Català / Czech / Deutsch / English / Castellano / Japanese / Dutch / Russian / Chinese
Doctype:HTML5
IDE: Sublime Text, Notepad++, FileZilla, PhotoshopTranslator: Daniel Kršiak
Twitter: @krsiakdaniel
Location: Czech Republic
ZH Translator: Ana Villalba
Location: Spain
JA Translator: Clémence Haure
Location: Spain
FR Translator: Thibaud Desodt
Location: Belgium
Media Queries by: Marta��?humanstxt.org
Here is the code I used:
Code: Select all
#include <string.h>
#include <stdlib.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_log.h"
#include "esp_system.h"
#include "nvs_flash.h"
#include "app_wifi.h"
#include "esp_http_client.h"
#define MAX_HTTP_RECV_BUFFER 512
static const char *TAG = "HTTP_CLIENT";
extern const char server_root_cert_pem_start[] asm("_binary_server_root_cert_pem_start");
extern const char server_root_cert_pem_end[] asm("_binary_server_root_cert_pem_end");
esp_err_t _http_event_handler(esp_http_client_event_t *evt)
{
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);
if (!esp_http_client_is_chunked_response(evt->client)) {
// Write out data
// printf("%.*s", evt->data_len, (char*)evt->data);
}
break;
case HTTP_EVENT_ON_FINISH:
ESP_LOGD(TAG, "HTTP_EVENT_ON_FINISH");
break;
case HTTP_EVENT_DISCONNECTED:
ESP_LOGD(TAG, "HTTP_EVENT_DISCONNECTED");
break;
}
return ESP_OK;
}
static void https_with_url()
{
esp_http_client_config_t config = {
.url = "http://humanstxt.org/humans.txt",
.event_handler = _http_event_handler
};
esp_http_client_handle_t client = esp_http_client_init(&config);
esp_err_t err = esp_http_client_perform(client);
if (err == ESP_OK) {
ESP_LOGI(TAG, "HTTPS Status = %d, content_length = %d",
esp_http_client_get_status_code(client),
esp_http_client_get_content_length(client));
} else {
ESP_LOGE(TAG, "Error perform http request %s", esp_err_to_name(err));
}
char charbuffer[2000];
int data_len = esp_http_client_read(client, &charbuffer, esp_http_client_get_content_length(client));
printf("data length %i\n", data_len);
printf("%s", charbuffer);
esp_http_client_cleanup(client);
}
static void http_test_task(void *pvParameters)
{
app_wifi_wait_connected();
ESP_LOGI(TAG, "Connected to AP, begin http example");
https_with_url();
ESP_LOGI(TAG, "Finish http example");
vTaskDelete(NULL);
}
void app_main()
{
esp_err_t ret = nvs_flash_init();
if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
ESP_ERROR_CHECK(nvs_flash_erase());
ret = nvs_flash_init();
}
ESP_ERROR_CHECK(ret);
app_wifi_initialise();
xTaskCreate(&http_test_task, "http_test_task", 8192, NULL, 5, NULL);
}