Page 1 of 1

Reading from news API server seems to have buffer overflow.

Posted: Mon Feb 05, 2024 3:35 pm
by twiereng
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


Code: Untitled.cpp Select all

// Retrieve news headlines from newsapi.org
// *****************************************************************************************************************
void getNewsFromInternet()
{
const char* server = "newsapi.org";
WiFiClientSecure client;
client.setInsecure();
const int httpPort = 443; // 80 is for HTTP / 443 is for HTTPS!
int httpCode, count = 0;
news_payload = " ";

Serial.println("\nStarting connection to server...");
httpCode = client.connect(server, 443);
Serial.println(httpCode);
if (httpCode == 0)
Serial.println("Connection failed!");
else {
Serial.println("Connected to server!");
// Make a HTTP request:
client.println("GET /v2/top-headlines?country=us&apiKey=myKey HTTP/1.1");
client.println("Host: newsapi.org");
client.println("Connection: close");
client.println("User-Agent: ArduinoWiFi/1.0"); // This is an example User-Agent; you can set it to anything relevant
client.println();

while (client.connected()) {
String line = client.readStringUntil('\n');
if (line == "\r") {
Serial.println("headers received");
break;
}
}
// if there are incoming bytes available
// from the server, read them and print them:
count = 0;
while (client.available()) {
n_payload[count++] = client.read();
// news_payload += c;
}
n_payload[count] = 0x00;
// Convert to String
news_payload = String(n_payload);
Serial.print("news_payload = ");
Serial.println(news_payload);
Serial.println(news_payload.length());
Serial.println(count);
client.stop();
}
}

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

Posted: Mon Feb 05, 2024 3:45 pm
by pipi61
Check these variable definitions:
news_payload
n_payload