1

I am trying to fetch some details from an API endpoint (https://bitcoin-ethereum-price-test.vercel.app/btc). But everytime it is returning false (-1). When I GET the endpoint on my browser it is just workign fin, returning 200.

http.GET() returns -1

serial monitor putput

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

WiFiClient wifiClient;

void setup() {
  Serial.begin(9600);
  WiFi.begin("56", "emayush56");
  while(WiFi.status() != WL_CONNECTED)
  {
    delay(200);
    Serial.print("..");
  }
  Serial.println();
  Serial.println("NodeMCU is connected!");
  Serial.println(WiFi.localIP());
}

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

    HTTPClient http;
    
    http.begin(wifiClient, "https://bitcoin-ethereum-price-test.vercel.app/btc");
    int httpCode = http.GET();
    Serial.println("***   RESPONSE STATUS   ***");
    Serial.println(httpCode);

    if (httpCode > 0) {
      String payload = http.getString();
      Serial.println(payload);
    }
    http.end();
  }
  delay(3000);
}

I think either I am doing something wrong with http.begin() or something else. http.begin() can be called in two different ways:

type1: bool begin(WiFiClient &client, const String& url);

type2: bool begin(WiFiClient &client, const String& host, uint16_t port, const String& uri = "/", bool https = false);

I have tried with both of them - first by passing directly the WifiClient object and the URL (type 1), and then (type2) by passing the WiFiClient object and other parameters.

If my main api endpoint (https://bitcoin-ethereum-price-test.vercel.app/btc) is returnig 200 then why http.GET() is returning false? Please help me identify the issue.

emAyush56
  • 41
  • 5
  • In C and C++, `false` is represented as 0. -1 doesn't mean `false`, it just indicates an error return from this method. – romkey Feb 09 '23 at 01:55

1 Answers1

0

You're making an HTTP request to an HTTPS API. You will need to use a certificate's sha1 fingerprint. You can get the fingerprint in chrome by clicking on the little lock by the beginning of the URL, go to "Connection is secure" option then "Certificate is valid" and it'll show you some info about the certificate with the keys at the bottom.
Here is some example code I found which uses HTTPS with the HTTPClient library:\

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


    HTTPClient http;

    Serial.print("[HTTPS] begin...\n");

    http.begin("https://some.secure_server.com/auth/authorise", "2F 2A BB 23 6B 03 89 76 E6 4C B8 36 E4 A6 BF 84 3D DA D3 9F");

    http.addHeader("Content-Type", "application/x-www-form-urlencoded");
    
    int httpCode = http.POST("user_id=mylogin&user_password=this%20is%20my%20%24ecret%20pa%24%24word");
    if (httpCode > 0) {
      http.writeToStream(&Serial);

      // HTTP header has been send and Server response header has been handled
      Serial.printf("[HTTP] ... code: %d\n", httpCode);

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

  delay(10000);
}
Roman
  • 124
  • 6
  • It throws compilation error at `http.beign()`, line:10. – emAyush56 Feb 10 '23 at 07:34
  • Idk if you're talking about the example code or your code. The code I posted was just an example you can reference, I wasn't expecting it to work the way it is. But it could help you find what you're looking for. Theres also this library you could try if that doesn't work https://github.com/espressif/arduino-esp32/tree/master/libraries/WiFiClientSecure – Roman Feb 10 '23 at 16:48