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.

  • Post your complete, working sketch so properly help you. – user31481 Oct 15 '17 at 16:01
  • @LookAlterno I have posted my complete code. – Ccr Oct 15 '17 at 16:06
  • I got an error in the linking phase. I have to check my environment. Working on it. – user31481 Oct 15 '17 at 21:29
  • AFAIK, ESP8266HTTPClient doesn't support HTTPS. Also, why are you using a Content-Type header together with the GET method? That doesn't make any sense, because GET requests don't have a body. – tttapa Oct 16 '17 at 7:47
  • I believe you need to supply the website certificate footprint to http.begin – Musa Oct 16 '17 at 15:28
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);
}
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.