COINMARKETCAP API DISCONNECTING

Hello,

i have a code for simple bitcoin ticker:

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

const char* ssid = "***";
const char* password = "***";
const String api_key = "***";

const char* symbol = "BTC";
const char* convert_to = "USD";

const char* host = "pro-api.coinmarketcap.com";
const int https_port = 443;

WiFiClientSecure client;

void setup() {
  Serial.begin(115200);
  delay(100);
  Serial.print("Connecting to ");
  Serial.println(ssid);

  WiFi.mode(WIFI_STA);
  WiFi.disconnect();
  delay(100);

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

  Serial.println("");
  Serial.println("WiFi connected!");

}
void loop() {
  if (!client.connect(host, https_port)) {
    Serial.println("Connection failed!");
    delay(60000);
    return;
  }

  String endpoint = "/v1/cryptocurrency/quotes/latest?CMC_PRO_API_KEY=" + api_key + "&symbol=" + symbol + "&convert=" + convert_to;

  Serial.print("Requesting: ");
  Serial.println(endpoint);

  client.print(String("GET ") + endpoint + " HTTP/1.1\r\n" +
               "Host: " + host + "\r\n" +
               //"User-Agent: ESP8266\r\n" +
               "Connection: close\r\n\r\n");

  if (client.println() == 0) {
    Serial.println("Failed to send request");
    delay(60000);
    return;
  }

  // check HTTP response
  char http_status[32] = {0};
  client.readBytesUntil('\r', http_status, sizeof(http_status));
  if (strcmp(http_status, "HTTP/1.1 200 OK") != 0) {
    Serial.print("Unexpected response: ");
    Serial.println(http_status);
    delay(60000);
    return;
  }

  // skip HTTP headers
  while (client.connected()) {
    String line = client.readStringUntil('\n');
    Serial.println(line);
    if (line == "\r") {
      break;
    }
  }

  // skip content length
  if (client.connected()) {
    String line = client.readStringUntil('\n');
    Serial.println(line);
  }

  // get response
  String response = "";
  while (client.connected()) {
    String line = client.readStringUntil('\n');
    Serial.println(line);
    line.trim();
    if (line != "\r") {
      response += line;
    }
  }

  client.stop();

  // parse response
  DynamicJsonDocument jsonDocument;
  DeserializationError error = deserializeJson(jsonDocument, response);
  if (error) {
    Serial.println("Deserialization failed");
    delay(60000);
    return;
  }
  JsonObject root = jsonDocument.as<JsonObject>();

  // check API status
  JsonObject status = root["status"];
  int status_error_code = status["error_code"];
  if (status_error_code != 0) {
    String status_error_message = status["error_message"];
    Serial.print("Error: ");
    Serial.println(status_error_message);
    delay(60000);
  }

  JsonObject coin = root["data"][symbol];
  String name = coin["name"];
  String updated = coin["last_updated"];
  Serial.println("Name: " + name);
  Serial.println("Last updated: " + updated);

  JsonObject quote = coin["quote"][convert_to];
  float price = quote["price"];
  Serial.print("Price: $");
  Serial.println(price, 2);

  delay(60000);
}

i uploaded it to the NodeMCU, but after uploading, the board will connect to the wifi and immediately after that, the board disconnect…

The WiFi AP is alright, i have tried another bitcoin ticker code (with coindesk provider), it runned without problems.

The WiFiClient library is alright too, i have tried this library with my previously created bitcoin ticker codes, but only with CoinMarketCap provider won’t to run. (json library has been tried with most of all versions, actually is used version 6.0.1, the newest json library version does not work)

I have tried to change the port from 443 to 80 and change the library from WiFiClientSecure.h to WiFiClient.h, in this case serial monitor shows an unexpected response: HTTP 1.1 301 Moved Permanently. Still not work. Any way to fix that please?

Thank you and have a nice day!

What do you get if you hit that endpoint from a browser?

From browser I get what I want. That is the expected values of bitcoin (price, 24h change, 1h change etc.). In main page of my CMC Accout is showed activity of using my api. There is showed every IP adress from which has been called a request with my api key. The adress of nodemcu has not been never showed yet.

What IP are you expecting to see for your nodemcu?