emAyush56
emAyush56

Reputation: 171

http.GET() sends false (-1) in esp8266 (arduino)

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.

Upvotes: 1

Views: 785

Answers (2)

mahboudz
mahboudz

Reputation: 39376

http.getSize() will return -1 if there is no "content-length" header. That can happen with certain response codes like 204, where no data is supposed to be return. Print out your response code to see why you are getting no data back and hence the "content-length" filed is omitted.

Upvotes: 0

Roman
Roman

Reputation: 144

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);
}

Upvotes: 0

Related Questions