Reading from news API server seems to have buffer overflow.

twiereng
Posts: 5
Joined: Thu Jan 10, 2019 8:07 pm

Reading from news API server seems to have buffer overflow.

Postby twiereng » Mon Feb 05, 2024 3:35 pm

I can receive the JSON string from the server with this function but it is truncated - the end is not a correct ending for a JSON string. I believe I have buffer overflow. How can I change the buffer size? Using Arduino IDE 2.2.1 and Espressif ESP32 board 2.0.14.

Thanks, Theron WIerenga


  1. // Retrieve news headlines from newsapi.org
  2. // *****************************************************************************************************************
  3. void getNewsFromInternet()
  4. {
  5.    const char* server = "newsapi.org";
  6.    WiFiClientSecure client;
  7.    client.setInsecure();
  8.    const int httpPort = 443; // 80 is for HTTP / 443 is for HTTPS!
  9.    int httpCode, count = 0;
  10.    news_payload = "        ";
  11.  
  12.   Serial.println("\nStarting connection to server...");
  13.   httpCode = client.connect(server, 443);
  14.   Serial.println(httpCode);
  15.   if (httpCode == 0)
  16.     Serial.println("Connection failed!");
  17.   else {
  18.     Serial.println("Connected to server!");
  19.     // Make a HTTP request:
  20.     client.println("GET /v2/top-headlines?country=us&apiKey=myKey HTTP/1.1");
  21.     client.println("Host: newsapi.org");
  22.     client.println("Connection: close");
  23.     client.println("User-Agent: ArduinoWiFi/1.0"); // This is an example User-Agent; you can set it to anything relevant
  24.     client.println();
  25.  
  26.     while (client.connected()) {
  27.       String line = client.readStringUntil('\n');
  28.       if (line == "\r") {
  29.         Serial.println("headers received");
  30.         break;
  31.       }
  32.     }
  33.     // if there are incoming bytes available
  34.     // from the server, read them and print them:
  35.     count = 0;
  36.     while (client.available()) {
  37.       n_payload[count++] = client.read();
  38.       // news_payload += c;
  39.     }
  40.     n_payload[count] = 0x00;
  41.     // Convert to String  
  42.     news_payload = String(n_payload);
  43.     Serial.print("news_payload = ");
  44.     Serial.println(news_payload);
  45.     Serial.println(news_payload.length());
  46.     Serial.println(count);
  47.     client.stop();
  48.   }
  49. }

pipi61
Posts: 60
Joined: Fri Dec 23, 2016 10:58 pm

Re: Reading from news API server seems to have buffer overflow.

Postby pipi61 » Mon Feb 05, 2024 3:45 pm

Check these variable definitions:
news_payload
n_payload

Who is online

Users browsing this forum: No registered users and 146 guests