API consumption returns -1 on ESP8266

Hello guys!

I'm trying to consume an API, but it doesn't work, it connects to the internet, but it returns 1 in the HTTPCODE

Do not enter an API URL for security reasons.

code:

#include <ESP8266WiFi.h>
#include <ArduinoJson.h>
#include <ESP8266HTTPClient.h>
#include <WiFiClient.h>

char* SSID = "iPhone";
char* SENHA = "gabi1234";

char* BASE_URL = "URLDAAPI";

void initSerial();
void initWiFi();

WiFiClient client;
HTTPClient http;

void setup() {
initSerial();
initWiFi();
}

void loop() {
makeRequest();
delay(2000);
}

void initSerial() {
Serial.begin(115200);
}

void initWiFi() {
delay(10);
Serial.println("Conectando-se em: " + String(SSID));

WiFi.begin(SSID, SENHA);
while (WiFi.status() != WL_CONNECTED) {
delay(100);
Serial.print(".");
}
Serial.println();
Serial.print("Conectado na Rede " + String(SSID) + " | IP => ");
Serial.println(WiFi.localIP());
}

String makeRequest()
{
http.begin(client,BASE_URL);
int httpCode = http.GET();
Serial.println(httpCode);

if (httpCode < 0) {
return "request error - " + httpCode;

}

if (httpCode != HTTP_CODE_OK) {
return "Diferente " + httpCode;
}

String response = http.getString();
http.end();

return response;
}

-1 is connection failed. it can't connect to host and port from your url

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