I'm using the ESP8266 board to connect to weather and news services to display on a MAX7219 panel. I've been able to access OpenWeatherMap.org and get their JSON feed. Next, I registered for an API key on the New York Times Developer network page and tried to access their JSON feed. Here's a short bit of code to test the API connection"
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <Arduino_JSON.h>
#include <ezTime.h>
const char* ssid = "xxxx";
const char* password = "xxxxxxxxxx";
String news_Api_Key = "xxxxxxxxxxxxxxxxxxxxx";
String myDT = "";
String json_array;
WiFiClient wifiClient;
void setup() {
Serial.begin(9600);
WiFi.begin(ssid, password);
Serial.println("Connecting to WIFI...");
while(WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("IP Address: ");
Serial.println(WiFi.localIP());
if (WiFi.status()== WL_CONNECTED){
String server = "http://api.nytimes.com/svc/topstories/v2/home.json?api-key=" + news_Api_Key;
json_array = GET_Request(server.c_str());
Serial.println(json_array);
JSONVar my_obj = JSON.parse(json_array);
if (JSON.typeof(my_obj) == "undefined") {
Serial.println("Parsing input failed!");
return;
}
} else {
Serial.println("WiFi Disconnected");
}
}
void loop() {
}
String GET_Request(const char* server) {
HTTPClient http;
http.begin(wifiClient,server);
int httpResponseCode = http.GET();
String payload = "{}";
if (httpResponseCode>0) {
Serial.print("HTTP Response code: ");
Serial.println(httpResponseCode);
payload = http.getString();
Serial.print(payload);
}
else {
Serial.print("Error code: ");
Serial.println(httpResponseCode);
}
http.end();
return payload;
}
When it connects, I receive
HTTP Response code: 200
Parsing input failed! -- so myObj is undefined.
If I use the server string in my regular browser, it's redirected to https, and the response shows the JSON text. So the api key works and so does the connect string. But if I replace the http with https in the String server= line, I get back
HTTP Response code: 400
The plain HTTP request was sent to HTTPS port
And, naturally, the NYT Developer Network page has no help on this at all. What can I do to fix the problem?