Thank you @ kenb4,
I also tried using ArduinoHttpClient, but no luck. Please check my code below—I tested it with two APIs; one is responding correctly, while the other is not.
Working Sample
#include <WiFiS3.h>
#include <ArduinoHttpClient.h>
#include <ArduinoJson.h>
// WiFi Credentials
const char* ssid = "Juansai";
const char* password = "0123456789";
// Server Info
const char* server = "fakestoreapi.com"; // Sample API
const int port = 443; // HTTPS port
WiFiSSLClient sslClient;
HttpClient httpClient(sslClient, server, port);
void setup() {
Serial.begin(9600);
while (!Serial)
;
Serial.println("Connecting to WiFi...");
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(1000);
}
Serial.println("\nWiFi connected!");
Serial.print("IP Address: ");
Serial.println(WiFi.localIP());
fetchData();
}
void fetchData() {
Serial.println("\nMaking HTTPS GET request with headers...");
String url = "/products/category/jewelery"; // Sample API endpoint
// Set custom headers
httpClient.beginRequest();
httpClient.get(url);
httpClient.sendHeader("Content-Type", "application/json");
httpClient.endRequest();
int statusCode = httpClient.responseStatusCode();
Serial.print("Status code: ");
Serial.println(statusCode);
while (httpClient.headerAvailable()) {
Serial.print(httpClient.readHeaderName());
Serial.print(": ");
Serial.println(httpClient.readHeaderValue());
}
if (statusCode != 200) {
Serial.println("Failed to get a valid response!");
return;
}
String response = httpClient.responseBody();
Serial.println("Response:");
Serial.println(response);
}
void loop() {
// Nothing here
}
Response
Not Working Sample
#include <WiFiS3.h>
#include <ArduinoHttpClient.h>
#include <ArduinoJson.h>
// WiFi Credentials
const char* ssid = "Juansai";
const char* password = "0123456789";
// Server Info
const char* server = "mobile.eautomates.com"; // Sample API
const int port = 443; // HTTPS port
WiFiSSLClient sslClient;
HttpClient httpClient(sslClient, server, port);
void setup() {
Serial.begin(9600);
while (!Serial)
;
Serial.println("Connecting to WiFi...");
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(1000);
}
Serial.println("\nWiFi connected!");
Serial.print("IP Address: ");
Serial.println(WiFi.localIP());
fetchData();
}
void fetchData() {
Serial.println("\nMaking HTTPS GET request with headers...");
String url = "/api/IOT/Arduino/GetRotation"; // Sample API endpoint
// Set custom headers
httpClient.beginRequest();
httpClient.get(url);
httpClient.sendHeader("Content-Type", "application/json");
httpClient.endRequest();
int statusCode = httpClient.responseStatusCode();
Serial.print("Status code: ");
Serial.println(statusCode);
while (httpClient.headerAvailable()) {
Serial.print(httpClient.readHeaderName());
Serial.print(": ");
Serial.println(httpClient.readHeaderValue());
}
if (statusCode != 200) {
Serial.println("Failed to get a valid response!");
return;
}
String response = httpClient.responseBody();
Serial.println("Response:");
Serial.println(response);
}
Response