5

I am trying to do a simple GET request. But I always get status code -1. Here is my complete code.

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

WiFiClient client;
#define AP_SSID "CCR"
#define AP_PASSWORD  "ccrxxxxx"

void wifiConnect() {
  Serial.print("Connecting to AP");
  WiFi.begin(AP_SSID, AP_PASSWORD);
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.print(".");
  }
}

void setup() {
  Serial.begin(9600);
  pinMode(0,1);
  Serial.begin(9600);
  delay(300);
  Serial.println("starting");
  WiFi.disconnect();
  Serial.println("disconnected");
  wifiConnect();
  Serial.println("Connected");
}

void loop() {
  HTTPClient http;
  http.begin("https://calm-falls-41696.herokuapp.com/api/v1/cards");
  http.addHeader("Content-Type", "application/json");
  int httpCode = http.GET();
  Serial.println(httpCode);
  if(httpCode == HTTP_CODE_OK) {
  Serial.print("HTTP response code ");
    Serial.println(httpCode);
    String response = http.getString();
    Serial.println(response);
  }
  http.end();
}

Wifi connection was successful. But in my serial terminal I always see -1. I am using PlatfromIO in Visual Studio code.

Update 1: The URL was corrected.

 http.begin("https://calm-falls-41696.herokuapp.com/api/v1/cards.json");

I tested it with google.com too. But it is still returning -1.

dda
  • 1,595
  • 1
  • 12
  • 17
Ccr
  • 279
  • 1
  • 4
  • 10

2 Answers2

4

As Musa said, I needed to suply the website certificate footprint to http.begin.

void loop() {
  HTTPClient http;
  String thumbprint="08:3B:71:72:02:43:6E:CA:ED:42:86:93:BA:7E:DF:81:C4:BC:62:30";
  String path = "https://calm-falls-41696.herokuapp.com/api/v1/cards.json";
  http.begin(path, thumbprint);
  int httpCode = http.GET();
  Serial.println(httpCode);
  if(httpCode == HTTP_CODE_OK) {
    Serial.print("HTTP response code ");
    Serial.println(httpCode);
    String response = http.getString();
    Serial.println(response);
 }
  http.end();
  delay(1000);
}
sOnt
  • 145
  • 1
  • 1
  • 8
Ccr
  • 279
  • 1
  • 4
  • 10
2

I tested your code only changing the URL and header, like this:

//  http.begin("http://calm-falls-41696.herokuapp.com/api/v1/cards");
  http.begin("http://bing.com");
  http.addHeader("Content-Type", "text/html");

And it return httpCode == 400.

As @tttapa said, it looks like the library doesn't support https.