"[HTTPS] GET... failed, error: connection refused" for certain API calls

Hello there
I am trying to use HTTPClient for NodeMCU, and i get the above mentioned error
Here’s my code:

  // wait for WiFi connection
  if ((WiFiMulti.run() == WL_CONNECTED)) {

    std::unique_ptr<BearSSL::WiFiClientSecure>client(new BearSSL::WiFiClientSecure);

    client->setFingerprint(fingerprint);

    HTTPClient https;

    Serial.print("[HTTPS] begin...\n");
    String linker = "https://xxxxxxxxxxxxxxxxxxxx.t/post?node=sadaf22&values={\"bbb\":111}";   // <------
    if (https.begin(*client,linker )) {  // HTTPS

      Serial.print("[HTTPS] GET...\n");
      // start connection and send HTTP header
      int httpCode = https.GET();

      // httpCode will be negative on error
      if (httpCode > 0) {
        // HTTP header has been send and Server response header has been handled
        Serial.printf("[HTTPS] GET... code: %d\n", httpCode);

        // file found at server
        if (httpCode == HTTP_CODE_OK || httpCode == HTTP_CODE_MOVED_PERMANENTLY) {
          String payload = https.getString();
          Serial.println(payload);
        }
      } else {
        Serial.printf("[HTTPS] GET... failed, error: %s\n", https.errorToString(httpCode).c_str());
      }

      https.end();
    }

When I run the link on the browser it works fine.

All the efforts are highly appreciated

Yep, in browser it will work, because it have Root CA certificate that issued certificate for that webserver.
You need to add it to your microcontroller too.
I remember WiFiClientSecure library is suing SHA1 fingerprint of public key of webserver..
But with BearSSL i think there you can use Root CA certificate for that connection (ESP32 is using for instance like that).

There can be also used INSECURED method for force HTTPS connection without certificate or fingerprint, but... It is not recommended (Insecured HTTPS).

When I run the link on the browser it works fine.

There are a lot of possible causes for this. One is that the NodeMCU doesn't have the same network connection (maybe it is blocked somewhere). Another may be a missing DNS server. Also possible is a IPv6 only server.

As you failed to post complete code we must also assume that the error might be in that part of the code you're hiding from us.

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