No Response to GET Request From Seeed Wio Terminal

There is no response from a GET request with the Wio Terminal to a specific API, api. ambeedata .com The code is modified from Seeed's example: Reading Github Repository Stats from Wio Terminal. The original Github example works fine. After modifying the code to pass my API key and use the api. ambeedata .com endpoints, the request doesn't return. However, a connection is established to the server. Testing the request in Postman with the same request returns the correct JSON response. The Realtek RTL8720 firmware is 2.1.2, and all dependant libraries are the most recent versions. (Arduino FS, rpcUnified, rpcWiFi, SFUD, mbedtls) Using a different Arduino board, I replicated the code and got the correct JSON response from api.ambeedata.com. Any recommendations on what to try next would be appreciated! The same question asked at stackoverflow, but still not getting any helping reply so I put here.
switches

#include <rpcWiFi.h>
#include <WiFiClientSecure.h>

const char* ssid     = "SSID";     // your network SSID
const char* password = "password"; // your network password

const char* server = "api.ambeedata.com";  // Server URL
const char* test_root_ca = \
                           "-----BEGIN CERTIFICATE-----\n"
                           //Removed for the brevity of the post
                           "-----END CERTIFICATE-----\n";

WiFiClientSecure client;

void setup() {
  //Initialize serial and wait for port to open:
  Serial.begin(115200);
  while (!Serial); // Wait for Serial to be ready
  delay(1000);

  // attempt to connect to Wifi network:
  while (WiFi.status() != WL_CONNECTED) {
    Serial.print("Attempting to connect to SSID: ");
    Serial.println(ssid);
    Serial.println(WiFi.status());
    WiFi.begin(ssid, password);
    Serial.println(WiFi.status());
    Serial.print(".");
    // wait 3 seconds for re-trying
    delay(3000);
    }
  
  Serial.print("Connected to ");
  Serial.println(ssid);

  client.setCACert(test_root_ca);

  Serial.println("\nStarting connection to server...");
  
  if (!client.connect(server, 443)) {
    Serial.println("Connection failed!");
    } 
  else {
    Serial.println("Connected to server!");
     
  //Send GET request to API
    client.println("GET https://api.ambeedata.com HTTP/1.0");
    client.println("Host: api.ambeedata.com");
    client.println("Connection: close");
    client.println("x-api-key: apikey");
    client.println("Content-type: application/json");
    client.println();

    Serial.println("request sent");

    while (client.connected()) {
      String line = client.readStringUntil('\n');
      if (line == "\r") {
        Serial.println("headers received");
        break;
      }
    }
  Serial.println("reply was:");
  Serial.println("==========");
  
  String line = client.readStringUntil('\n');
        if (line == "\r") {
            Serial.println("headers received");
        }
  
    // if there are incoming bytes available
    // from the server, read them and print them:
    while (client.available()) {
      char c = client.read();
      if (c == '\n') {
        Serial.write('\r');
      }
      Serial.write(c);
    }
    client.stop();
  }
}

void loop() {
  // do nothing
}

I tried this

#include <WiFi.h>
#include <WiFiClientSecure.h>
#include <ArduinoJson.h>

const char* ssid = "xxxx";
const char* password = "xxx";
const char* test_root_ca = \
                           "-----BEGIN CERTIFICATE-----\n" \
                           "MIIDQTCCAimgAwIBAgITBmyfz5m/jAo54vB4ikPmljZbyjANBgkqhkiG9w0BAQsF\n" \
                           "ADA5MQswCQYDVQQGEwJVUzEPMA0GA1UEChMGQW1hem9uMRkwFwYDVQQDExBBbWF6\n" \
                           "b24gUm9vdCBDQSAxMB4XDTE1MDUyNjAwMDAwMFoXDTM4MDExNzAwMDAwMFowOTEL\n" \
                           "MAkGA1UEBhMCVVMxDzANBgNVBAoTBkFtYXpvbjEZMBcGA1UEAxMQQW1hem9uIFJv\n" \
                           "b3QgQ0EgMTCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBALJ4gHHKeNXj\n" \
                           "ca9HgFB0fW7Y14h29Jlo91ghYPl0hAEvrAIthtOgQ3pOsqTQNroBvo3bSMgHFzZM\n" \
                           "9O6II8c+6zf1tRn4SWiw3te5djgdYZ6k/oI2peVKVuRF4fn9tBb6dNqcmzU5L/qw\n" \
                           "IFAGbHrQgLKm+a/sRxmPUDgH3KKHOVj4utWp+UhnMJbulHheb4mjUcAwhmahRWa6\n" \
                           "VOujw5H5SNz/0egwLX0tdHA114gk957EWW67c4cX8jJGKLhD+rcdqsq08p8kDi1L\n" \
                           "93FcXmn/6pUCyziKrlA4b9v7LWIbxcceVOF34GfID5yHI9Y/QCB/IIDEgEw+OyQm\n" \
                           "jgSubJrIqg0CAwEAAaNCMEAwDwYDVR0TAQH/BAUwAwEB/zAOBgNVHQ8BAf8EBAMC\n" \
                           "AYYwHQYDVR0OBBYEFIQYzIU07LwMlJQuCFmcx7IQTgoIMA0GCSqGSIb3DQEBCwUA\n" \
                           "A4IBAQCY8jdaQZChGsV2USggNiMOruYou6r4lK5IpDB/G/wkjUu0yKGX9rbxenDI\n" \
                           "U5PMCCjjmCXPI6T53iHTfIUJrU6adTrCC2qJeHZERxhlbI1Bjjt/msv0tadQ1wUs\n" \
                           "N+gDS63pYaACbvXy8MWy7Vu33PqUXHeeE6V/Uq2V8viTO96LXFvKWlJbYK8U90vv\n" \
                           "o/ufQJVtMVT8QtPHRh8jrdkPSHCa2XV4cdFyQzR1bldZwgJcJmApzyMZFo6IQ6XU\n" \
                           "5MsI+yMRQ+hDKXJioaldXgjUkK642M4UwtBV8ob2xJNDd2ZhwLnoQdeXeGADbkpy\n" \
                           "rqXRfboQnoZsG4q5WTP468SQvvG5\n" \
                           "-----END CERTIFICATE-----\n";

WiFiClientSecure client;

void setup() {
  Serial.begin(115200);
  while (!Serial); // Wait for Serial to be ready

  // attempt to connect to Wifi network:
  while (WiFi.status() != WL_CONNECTED) {
    Serial.print("Attempting to connect to SSID: ");
    Serial.println(ssid);
    Serial.println(WiFi.status());
    WiFi.begin(ssid, password);
    Serial.println(WiFi.status());
    Serial.print(".");
    // wait 3 seconds for re-trying
    delay(3000);
  }

  Serial.print("Connected to "); Serial.println(ssid);

  client.setCACert(test_root_ca);
  Serial.println("\nStarting connection to server...");
  if (!client.connect("api.ambeedata.com", 443)) {
    Serial.println("Connection failed!");
  } else {
    Serial.println("Connected to server");

    client.println("GET https://api.ambeedata.com HTTP/1.0");
    client.println("Host: api.ambeedata.com");
    client.println("User-Agent: ESP32");
    client.println("Content-Type: application/json; charset=utf-8");
    client.println("Connection: close");
    client.println();

    char c;
    bool currentLineIsEmpty = false;
    while (client.connected()) {
      if (client.available()) {
        c = client.read();
        if (c != '\n') {
          if (c != '\r') currentLineIsEmpty = false;
        } else if (currentLineIsEmpty) break;
        else currentLineIsEmpty = true;
      }
    }
    StaticJsonDocument<400> doc;
    deserializeJson(doc, client);
    client.stop();
    Serial.println("Received the following JSON");
    serializeJsonPretty(doc, Serial);
  }
}

void loop() {}

and it seems to work, I got back

Starting connection to server...
Connected to server
Received the following JSON
{
  "message": "working"
}
1 Like

@sh112imran, your topic has been moved to a more suitable location on the forum.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.