Skip to content

Make a GET request to a Dataverse

Miguel Tomas Silva edited this page Oct 15, 2022 · 10 revisions

To make a custom GET request to a Dataverse one needs to call the function method String GetInfoFromDataverse(String url);

In the following piece of code a GET request is made to request the lock state on a specific dataset id (unsigned long):


Dataverse dataverse = new Dataverse();

String id="dataset id";
String url="/api/datasets/"+ id +"/locks";

String rawResponse = dataverse::GetInfoFromDataverse(url);

The function method returns a string with the `rawResponse` from the `dataverse` server.

Next rawResponse can be digested into a JSON using the appropriate Json class library specific to the platform one is coding. On embedded firmware programming of smart devices, the ArduinoJson library must be included in the project (#include <ArduinoJson.h>). JSON serialization of the response string can be done like in the following piece of code


      bool uploadStatusNotOK=true;
      String rawResponse = GetInfoFromDataverse("/api/datasets/"+ datasetInfoJson["data"]["id"] +"/locks");
      const size_t capacity =2*rawResponse.length() + JSON_ARRAY_SIZE(1) + 7*JSON_OBJECT_SIZE(1);
      DynamicJsonDocument datasetLocksJson(capacity);  
      // Parse JSON object
      DeserializationError error = deserializeJson(datasetLocksJson, rawResponse);
      if (error) {
        mserial.printStr("unable to retrive dataset lock status. Upload not possible. ERR: "+error.f_str());
        //mserial.printStrln(rawResponse);
        return;
      }else{
         String stat = datasetInfoJson["status"];
         if(datasetInfoJson.containsKey("lockType")){
           String locktype = datasetInfoJson["data"]["lockType"];           
           mserial.printStrln("There is a Lock on the dataset: "+ locktype); 
           mserial.printStrln("Upload of most recent data is not possible without removal of the lock.");  
           // Do unlocking 
           // ...     
         }else{
            mserial.printStrln("The dataset is unlocked. Upload possible.");
            uploadStatusNotOK=false;     
         }
      }
    }
  

More info about this particular library can be found at the ArduinoJson website here.

Clone this wiki locally