Hello,
I have implemented code to call Youtube API. The call works fine and now I wish to deserialize the Json response. My working code is as follow:
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <WiFiClientSecureBearSSL.h>
// Replace with your network credentials
const char* ssid = "xxxx";
const char* password = "xxxx";
void setup() {
Serial.begin(115200);
//Serial.setDebugOutput(true);
Serial.println();
Serial.println();
Serial.println();
//Connect to Wi-Fi
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
Serial.print("Connecting to WiFi ..");
while (WiFi.status() != WL_CONNECTED) {
Serial.print('.');
delay(1000);
}
}
void loop() {
// wait for WiFi connection
if ((WiFi.status() == WL_CONNECTED)) {
std::unique_ptr<BearSSL::WiFiClientSecure>client(new BearSSL::WiFiClientSecure);
// Ignore SSL certificate validation
client->setInsecure();
//create an HTTPClient instance
HTTPClient https;
//Initializing an HTTPS communication using the secure client
Serial.print("[HTTPS] begin...\n");
if (https.begin(*client, "https://www.googleapis.com/youtube/v3/channels?part=statistics&id=xxxx")) { // HTTPS
Serial.print("[HTTPS] GET...\n");
// start connection and send HTTP header
https.addHeader("X-goog-api-key", "xxxx");
int httpCode = https.GET();
// httpCode will be negative on error
if (httpCode > 0) {
// HTTP header has been send and Server response header has been handled
Serial.printf("[HTTPS] GET... code: %d\n", httpCode);
// file found at server
if (httpCode == HTTP_CODE_OK || httpCode == HTTP_CODE_MOVED_PERMANENTLY) {
Serial.println(https.getString());
}
} else {
Serial.printf("[HTTPS] GET... failed, error: %s\n", https.errorToString(httpCode).c_str());
}
https.end();
} else {
Serial.printf("[HTTPS] Unable to connect\n");
}
}
Serial.println();
Serial.println("Waiting 2min before the next round...");
delay(120000);
}
Here is the response I get with the code above:
{
"kind": "youtube#channelListResponse",
"etag": "xxxxxxxxxxxxxxxxxxxxxxxxxxxx",
"pageInfo": {
"totalResults": 1,
"resultsPerPage": 5
},
"items": [
{
"kind": "youtube#channel",
"etag": "xxxxxxxxxxxxxxxxxxxxxxxxxxxx",
"id": "xxxxxxxxxxxxxxxxxxxxxxxxxxxx",
"statistics": {
"viewCount": "14025",
"subscriberCount": "46",
"hiddenSubscriberCount": false,
"videoCount": "50"
}
}
]
}
Thus I added the following code to process the Json response:
#include <ArduinoJson.h>
...
if (httpCode == HTTP_CODE_OK || httpCode == HTTP_CODE_MOVED_PERMANENTLY) {
Serial.println(https.getString());
String payload = https.getString();
JsonDocument doc;
DeserializationError error = deserializeJson(doc, payload);
if (error) {
Serial.print("deserializeJson() returned ");
Serial.println(error.c_str());
return;
}
const char* subscriberCount = doc["items"][0]["subscriberCount"];
Serial.print(subscriberCount);
}
After this change the call does not work anymore.
...[HTTPS] begin...
[HTTPS] GET...
[HTTPS] GET... failed, error: connection failed
I read that it is not a good practice to use String.
I have tried to replace String payload by char payload[500] = "";
This did not help and anyway the type returned by https.getString() is a String.
What could be wrong ?