Hi, I am trying to call the apps script API using ESP8266, the esp receives a response code of -11 and an empty body. In the below code snippet i have used GET method, I tried using POST but it still doesn't work. It works when I directly put the url in the browser search box. But can't figure out whats the issue here is.
CODE:
#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>
#include <ESP8266HTTPClient.h>
WiFiClientSecure wifiClient;
String apiUrl = "https://script.google.com/macros/s/AKfycbwH6BV5f751VwJIsZ2G3P9N7MblS65USOqTez-zPu_ga-tH9AGUoEk4LhsJm9NoJfeEIA/exec";
void setup(){
Serial.begin(115200);
WiFi.begin("SSID", "Password");
Serial.println("Connecting to WiFi...");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.println(".");
}
Serial.println("Connected to WiFi!");
delay(5000);
Serial.println("Calling API...1");
callAPI_GET("create");
delay(30000);
Serial.println("Calling API...2");
callAPI_GET("update");
delay(30000);
Serial.println("Calling API...3");
callAPI_GET("read");
}
void loop(){
delay(10000);
}
String getApiUrl(String method){
if (method=="create"){return apiUrl + "?method=create&id=D001&name=omm"; }
else if (method=="update"){return apiUrl + "?method=update&id=D001&data=111110000022222111110000022222"; }
else return apiUrl + "?method=read&id=D001";
}
void callAPI_GET(String method) {
Serial.println(method);
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
wifiClient.setInsecure();
int httpResponseCode;
// Prepare JSON payload
String url = getApiUrl(method);
Serial.println("New url: " + url);
http.begin(wifiClient, url); // Use the apiUrl variable
// http.addHeader("Content-Type", "text/plain;charset=utf-8");
http.addHeader("Content-Type", "application/x-www-form-urlencoded");
// Send POST request
httpResponseCode = http.GET();
Serial.println("GET request sent.");
Serial.println("Response Code = " + String(httpResponseCode));
Serial.println("Response Body = " + http.getString());
http.end();
} else {
Serial.println("WiFi not connected");
}
}
OUTPUT:
Connected to WiFi!
Calling API...1
create
New url: https://script.google.com/macros/s/AKfycbwH6BV5f751VwJIsZ2G3P9N7MblS65USOqTez-zPu_ga-tH9AGUoEk4LhsJm9NoJfeEIA/exec?method=create&id=D001&name=omm
GET request sent.
Response Code = -11
Response Body =
Calling API...2
update
New url: https://script.google.com/macros/s/AKfycbwH6BV5f751VwJIsZ2G3P9N7MblS65USOqTez-zPu_ga-tH9AGUoEk4LhsJm9NoJfeEIA/exec?method=update&id=D001&data=111110000022222111110000022222
GET request sent.
Response Code = -11
Response Body =
Calling API...3
read
New url: https://script.google.com/macros/s/AKfycbwH6BV5f751VwJIsZ2G3P9N7MblS65USOqTez-zPu_ga-tH9AGUoEk4LhsJm9NoJfeEIA/exec?method=read&id=D001
GET request sent.
Response Code = -11
Response Body =