Issues to get HTTP payload

Hey,
I really reviewied all examples available in internet and on this forum, but cannot really find proper answer or something that will work (looks like some examples work with some websites, but not with mine, or with www.google.com etc), and I think that code was working long time ago, and now it stopped:

#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <WiFiClientSecureBearSSL.h>

void setup() {
  Serial.begin(115200);
  Serial.println(F("\n\r* * * ESP BOOT * * *"));
  Serial.println(F("WiFi begin!"));
  WiFi.mode(WIFI_STA);
  WiFi.begin("xxxx", "xxxx");

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println(F("\n\rWiFi connected!"));
}

void getpr24h() {
  std::unique_ptr<BearSSL::WiFiClientSecure>client(new BearSSL::WiFiClientSecure);
  client->setInsecure();
  HTTPClient https;
  client->setTimeout(4000);

  
  if (https.begin(*client, "https://www.knappe.pl/aaa.html")) {  // HTTPS
    Serial.println("[HTTPS] GET...");
    int httpCode = https.GET();

    // httpCode will be negative on error
    if (httpCode > 0) {
      // HTTP header has been send and Server response header has been handled
      Serial.printf("[HTTPS] GET... code: %d\n", httpCode);
      // file found at server?
      if (httpCode == HTTP_CODE_OK) {
        const String& payload = https.getString();
        Serial.println(String("[HTTPS] Received payload: ") + payload);
        

      }
    } else {
      Serial.printf("[HTTPS] GET... failed, error: %s\n\r", https.errorToString(httpCode).c_str());
    }

    https.end();
  } else {
    Serial.printf("[HTTPS] Unable to connect\n\r");
  }
}

void loop() {
  getpr24h();
  Serial.println("Wait 20s before next round to not get banned on API server...");
  delay(20000);
}

and response I'm getting is:

WiFi begin!
....................

WiFi connected!
[HTTPS] GET...
[HTTPS] GET... code: 200
[HTTPS] Received payload: 
Wait 20s before next round to not get banned on API server...
[HTTPS] GET...
[HTTPS] GET... code: 200
[HTTPS] Received payload: 
Wait 20s before next round to not get banned on API server...
[HTTPS] GET...
[HTTPS] GET... code: 200
[HTTPS] Received payload: 

I tried HTTP, HTTPS, json file, HTML file, and it doesn't work.

Fixed it with:

        String payload = "";
        int len = http.getSize();
        uint8_t buff[128] = { 0 };
        WiFiClient * stream = http.getStreamPtr();

                // read all data from server
                while(http.connected() && (len > 0 || len == -1)) {
                    // get available data size
                    size_t size = stream->available();

                    if(size) {
                        // read up to 128 byte
                        int c = stream->readBytes(buff, ((size > sizeof(buff)) ? sizeof(buff) : size));

                        // write it to Serial
                        Serial.write(buff,c);
                        
                        //payload += String((char*)buff);
                        char charBuff[c + 1]; // Create a character array with space for null terminator
                        memcpy(charBuff, buff, c); // Copy the data to the character array
                        charBuff[c] = '\0'; // Null-terminate the character array
                        payload += String(charBuff); // Append the character array to the payload

                        if(len > 0) {
                            len -= c;
                        }
                    }
                    delay(1);
                }
        

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.