httpclientarduino-esp8266

Error: call to 'HTTPClient::begin' declared with attribute error: obsolete API, use ::begin(WiFiClient, url)


I tried to do a Clock News Weather Scrolling Marquee with the esp8266. But when I upload the code it's got an error. Can you help me? Here's a part of the code:( under the MIT License (Copyright 2018 David Payne))

  void PiHoleClient::getPiHoleData(String server, int port) {

  errorMessage = "";
  String response = "";

  String apiGetData = "http://" + server + ":" + String(port) + "/admin/api.php?summary";
  Serial.println("Sending: " + apiGetData);
  HTTPClient http;  //Object of class HTTPClient
  http.begin(apiGetData);// get the result (**the error code**)
  int httpCode = http.GET();
  //Check the returning code
  if (httpCode > 0) {
    response = http.getString();
    http.end();   //Close connection
    if (httpCode != 200) {
      // Bad Response Code
      errorMessage = "Error response (" + String(httpCode) + "): " + response;
      Serial.println(errorMessage);
      return;  
    }

error: exit status 1 call to 'HTTPClient::begin' declared with attribute error: obsolete API, use ::begin(WiFiClient, url)


Solution

  • You need to also create a new instance of WiFiClient from WiFiClient.h, and pass that in to the begin:

    #include <WiFiClient.h>
    
    WiFiClient wifiClient;
    
    void PiHoleClient::getPiHoleData(String server, int port) {
    
      errorMessage = "";
      String response = "";
    
      String apiGetData = "http://" + server + ":" + String(port) + "/admin/api.php?summary";
      Serial.println("Request: " + apiGetData);
      HTTPClient http;  //Object of class HTTPClient
      http.begin(wifiClient, apiGetData);// get the result (**the error code**)
      int httpCode = http.GET();
      //Check the returning code
      if (httpCode > 0) {
        response = http.getString();
        http.end();   //Close connection
        if (httpCode != 200) {
          // Bad Response Code
          errorMessage = "Error response (" + String(httpCode) + "): " + response;
          Serial.println(errorMessage);
          return;  
        }