ESP8266 returns -1 in get request

String host = "api.api-ninjas.com";
String path = "/v1/quotes?category=love";
String url = "https://api.api-ninjas.com/v1/quotes?category=love";

void setup() {
  Serial.begin(9600);
  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println("Connecting to WiFi...");
  }
  Serial.println("Connected to WiFi");
  Serial.print("Ip Address: ");
  Serial.println(WiFi.localIP());

  getQuote();
}

void loop(){

}

void getQuote(){
  WiFiClientSecure wifiClient;
  HTTPClient httpClient;

  if (!httpClient.begin(wifiClient, url)){
    Serial.println("Connection Failed! Aborting...");
    return;
  };
  Serial.print("connecting to ");
  Serial.println(host);

  int respCode = httpClient.GET();
  Serial.print("Got http status code: ");
  Serial.println(respCode);
  Serial.println(httpClient.getString());
  Serial.println(httpClient.errorToString(respCode));
  
}

Got this in Serial Monitor:

20:50:44.118 -> Connecting to WiFi...
20:50:45.099 -> Connecting to WiFi...
20:50:46.828 -> Connecting to WiFi...
20:50:47.830 -> Connecting to WiFi...
20:50:48.839 -> Connecting to WiFi...
20:50:49.825 -> Connecting to WiFi...
20:50:49.825 -> Connected to WiFi
20:50:49.859 -> Ip Address: 192.168.0.163
20:50:49.911 -> connecting to api.api-ninjas.com
20:50:50.010 -> Got http status code: -1
20:50:50.010 -> 
20:50:50.010 -> connection failed

What did i do wrong? I suspect of course it cant connect to the api at the first place, but why? maybe the the wifiClient & url arguments?? I'm clueless. Appreciated if someone can help :smiley:

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