How to call an IoT Central https REST API GET operator from an ESP8266

Kizito Kabanza 1 Reputation point
2022-02-09T04:13:33.437+00:00

I am currently developing on an ESP8266 in order to interact with the IoT Central devices. I would like to fetch, via HTTPS, telemetry, and parameters of certain Devices from the IoT Central REST API.

However, there are no official or concrete libraries on how to do so (examples, code samples, etc). When using provided examples of Arduino httpClient or ESPWifi libraries, it always generates an error due to the authentication barrier.

The header ''Authorization: SAS Token...'' prior to the GET command also didn't work.

Thanks

Azure App Services
Azure App Services
A feature of Azure App Service used to create and deploy scalable, mission-critical web apps.
4,551 questions
Azure IoT Central
Azure IoT Central
An Azure hosted internet of things (IoT) application platform.
265 questions
Microsoft Partner Center API
Microsoft Partner Center API
Microsoft Partner Center: A Microsoft website for partners that provides access to product support, a partner community, and other partner services.API: A software intermediary that allows two applications to interact with each other.
266 questions
{count} votes

3 answers

Sort by: Most helpful
  1. Roman Kiss 2,021 Reputation points
    2022-02-09T07:52:30.133+00:00

    Hi,

    Use the Postman client for test purpose to see that your GET request will work.

    The following screen snippet shows an example for getting a telemetry data:

    172465-image.png

    Note, that the Authorization header is the API token from the IoT Central App.

    Thanks
    Roman


  2. Kizito Kabanza 1 Reputation point
    2022-02-10T19:18:51.09+00:00

    I tried again with this following logic:

      // wait for WiFi connection  
      if ((WiFiMulti.run() == WL_CONNECTED)) {  
      
        WiFiClient client;  
      
        HTTPClient http;  
      
        Serial.print("[HTTP] begin...\n");  
        if (http.begin(client, "https://....")) {  // HTTP  
          http.setAuthorization("Authorization: SharedAccessSignature sr=7...");  
          http.addHeader(F("Content-Type"), "application/json");        
      
      
          Serial.print("[HTTP] GET...\n");  
          // start connection and send HTTP header  
          int httpCode = http.GET();  
      
          // httpCode will be negative on error  
          if (httpCode > 0) {  
            // HTTP header has been send and Server response header has been handled  
            Serial.printf("[HTTP] GET... code: %d\n", httpCode);  
      
            // file found at server  
            if (httpCode == HTTP_CODE_OK || httpCode == HTTP_CODE_MOVED_PERMANENTLY) {  
              const String& payload = http.getString();  
              Serial.println(payload);  
              Serial.println('DEBUG__________');            
              remoteSwitch = payload;  
            }  
          } else {  
            Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());  
          }  
      
          http.end();  
        } else {  
          Serial.printf("[HTTP} Unable to connect\n");  
        }  
      }  
    

    However, still ended as an error:

    173198-image.png

  3. Johnson Yang 81 Reputation points
    2022-02-10T19:32:32.927+00:00

    Hi,

    There is one place I can see that you didn't pass the fingerprint for HTTPS request in line 9, you can follow the discussion here: https://forum.arduino.cc/t/esp8266-httpclient-library-for-https/495245.

    You can also try to use the same way "content-type" header to add "Authorization" header.

    Hope this will help.

    Br,
    Johnson