HTTPS library pulls wrong characters?

Andrew12345
Posts: 2
Joined: Sat Aug 17, 2019 8:34 pm

HTTPS library pulls wrong characters?

Postby Andrew12345 » Sat Aug 17, 2019 8:51 pm

I'm getting a strange error here. I have the following code to read a short HTTPS page.

The page is generated on my server using flask, and going to it in a web browser prints "test phrase True"
curl <test page url> gives the same output.

When I run the following code on the ESP32, it spits out:
"Data read: test phrase Tru@0"

What do I have wrong here?
  1. ESP_LOGI(TAG, "Checking Test Page");
  2. esp_http_client_config_t http_config = {
  3.         .url = CHECK_URL,
  4.         .event_handler = _http_event_handler,
  5.         .cert_pem = (char *)server_cert_pem_start
  6.     };
  7.    
  8. esp_http_client_handle_t client = esp_http_client_init(&http_config);
  9. esp_err_t err = esp_http_client_perform(client);
  10.  
  11. if (err == ESP_OK) {
  12.     int statuscode = esp_http_client_get_status_code(client);
  13.     int length = esp_http_client_get_content_length(client);
  14.     ESP_LOGI(TAG, "Status = %d, content_length = %d",statuscode, length);
  15.     char readbuffer[length];
  16.     err = esp_http_client_read(client, readbuffer, length);
  17.     ESP_LOGI(TAG, "Data read: %s", readbuffer);

ESP_Sprite
Posts: 9769
Joined: Thu Nov 26, 2015 4:08 am

Re: HTTPS library pulls wrong characters?

Postby ESP_Sprite » Sun Aug 18, 2019 5:52 am

If anything, esp_http_client_read doesn't zero-terminate the string it receives; instead, it returns its length as the return value. ESP_LOG, however, assumes the string is zero-terminated. As it is not, ESP_LOG will happily print any crap that's after the end of the string, including perhaps a backspace character that eats up your 'e'.

Who is online

Users browsing this forum: jimmy98035@gmail.com and 109 guests