Http Get (from public API .json) not work. Status code -1

Hi, I’m trying to use data from a public API (.json) using this code, but I’m getting connection failed (statuscode -1) and I don’t know where could be the problem:

#include <WiFiManager.h>
#include <Arduino.h>

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

#include <MD_Parola.h>
#include <MD_MAX72xx.h>
#include <SPI.h>
const char* ssid = "ssid";
const char* password = "password";
char            chBuffer[128];
void setup() 
{
  Serial.begin(115200);
  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) 
  {
    delay(1000);
    Serial.println("Connecting...");
  }
}

void loop() 
{
  if ((WiFi.status() == WL_CONNECTED))

  {
    HTTPClient http;

    http.begin ("https://api.coingecko.com/api/v3/simple/price?ids=bitcoin%2Cethereum&vs_currencies=eur%2Cgbp%2Cusd&include_24hr_change=true");
    delay(6000);
    int httpCode = http.GET();

    if (httpCode > 0) {

    }

    Serial.println ("\nStatuscode: " + String(httpCode));
    delay (100);




    const size_t capacity = JSON_OBJECT_SIZE(2) + 2 * JSON_OBJECT_SIZE(6) + 150;
    DynamicJsonDocument doc(capacity);
    String payload = http.getString();
    const char* json = payload.c_str();


    deserializeJson(doc, json);

    JsonObject bitcoin = doc["bitcoin"];
    float bitcoin_eur = bitcoin["eur"]; // 9473.3
    float bitcoin_eur_24h_change = bitcoin["eur_24h_change"]; // 11.379516678954898
    float bitcoin_gbp = bitcoin["gbp"]; // 8642.89
    float bitcoin_gbp_24h_change = bitcoin["gbp_24h_change"]; // 11.58637677393075
    float bitcoin_usd = bitcoin["usd"]; // 11140.6
    float bitcoin_usd_24h_change = bitcoin["usd_24h_change"]; // 12.464050392648252

    JsonObject ethereum = doc["ethereum"];
    float ethereum_eur = ethereum["eur"]; // 276.02
    float ethereum_eur_24h_change = ethereum["eur_24h_change"]; // 3.5689620753981264
    float ethereum_gbp = ethereum["gbp"]; // 251.82
    float ethereum_gbp_24h_change = ethereum["gbp_24h_change"]; // 3.7613159836416026
    float ethereum_usd = ethereum["usd"]; // 324.6
    float ethereum_usd_24h_change = ethereum["usd_24h_change"]; // 4.577442219792744



    Serial.println("-------------------------------");

    Serial.print("EUR: ");
    Serial.println(bitcoin_eur);
    Serial.print("EUR 24hr %: ");
    Serial.println(bitcoin_eur_24h_change);

    Serial.print("GBP: ");
    Serial.println(bitcoin_gbp);
    Serial.print("GBP 24hr %: ");
    Serial.println(bitcoin_gbp_24h_change);

    Serial.print("USD: ");
    Serial.println(bitcoin_usd);
    Serial.print("USD 24hr %: ");
    Serial.println(bitcoin_gbp_24h_change);

    Serial.print("EUR: ");
    Serial.println(ethereum_eur);
    Serial.print("EUR 24hr %: ");
    Serial.println(ethereum_eur_24h_change);

    Serial.print("GBP: ");
    Serial.println(ethereum_gbp);
    Serial.print("GBP 24hr %: ");
    Serial.println(ethereum_gbp_24h_change);

    Serial.print("USD: ");
    Serial.println(ethereum_usd);
    Serial.print("USD 24hr %: ");
    Serial.println(ethereum_usd_24h_change);

    Serial.println("-------------------------------");

    http.end();
  }
  delay(10000);
}

If I try to access from web brower I get data without problems.

Could someone help me, please?

Thanks.

Kind regards

Someone can help me, please?

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