Skip to main content
1-Visitor
February 20, 2017
Solved

Looking for simple REST example to update thing property via an external GET call

  • February 20, 2017
  • 12 replies
  • 9952 views

Hello,

I looked through the community discussion topics and did not find a simple example. Forgive me if one is there and I missed it.

I am using TI Sensortags and have an external REST-accessible resource where I can get sensor values.

I want to model these as ThingWorx things and then update the properties via REST GET calls to the external resource.

The REST calls return simple JSON results.

Is there a simple code snippet someone can offer that does this: Calls the REST GET and parses the JSON to find the name, value pair and use the value to update the property value in ThingWorx?

Is there a good document I can consult that covers this? If so, where can I find it?

I have a developer account.

Thanks,

Jim

Best answer by jamesso

I figured this out.

The main problem was the cookie creation service needed to be in the PostText form rather than the PostJSON. The external server wouldn't allow the api token that is used as input to create a session cookie that matches the access rights of the user providing the api token to be provided in JSON form.

The code I wound up using is basically:

var params = {

    headers: undefined,

    withCookies: true /* BOOLEAN */,

    url: "https://0ac48bf3-9fab-4bad-8455-e394808eda6b.beta.thsq.io/0/session/" /* STRING */,

    content: "token=6f9328dfb0916e50c79d2d6e352d10f5" ,

    contentType: "application/x-www-form-urlencoded",

};

var result = Resources["ContentLoaderFunctions"].PostText(params);

me.thsqCookie = result.split('^')[0];

This is because the returned string includes the cookie and the trailing string ^user-ok^. So I had to split the cookie off.


Then the Get Service logic whose code I included above works because I had access rights to the external thing whose value I wanted to retrieve. The me.LastTemperaturre setting can be uncommented and it works.


Thanks for allowing me to present my situation. The new wrinkle that did not appear in any other comment was how PostText might be appropriate.

12 replies

5-Regular Member
February 20, 2017

Hi James,

Just to check have you already tried the following two great blog posts with detail and examples :

REST API Overview and Examples

ThingWorx REST API Cheat Sheet

jamesso1-VisitorAuthor
1-Visitor
February 20, 2017

I did see them but they seem focused on using REST when the endpoint is a ThingWorx server. In my case, the REST endpoint is an external server. The data I want to use is in the JSON returned from the external REST call. I was hoping for some Javascript code I could modify and then cut and paste into the definition of the ThingWorx representation of the Sensortag.

Sample JSON result:

{

  "value": "42.23",

  "time": 1487606556741

}

I want to use 42.23 as the new property value.

The form of the REST call is:

https://<<serveraddresshere>>/0/devices/<<deviceidhere>>/s/t

Where of course I have real strings for the server address and the device id.

5-Regular Member
February 21, 2017

Hi James,

I guess, below code is what you are looking for

var params = {
proxyScheme: undefined /* STRING */,
headers: undefined /* JSON */,
ignoreSSLErrors: undefined /* BOOLEAN */,
useNTLM: undefined /* BOOLEAN */,
workstation: undefined /* STRING */,
useProxy: undefined /* BOOLEAN */,
withCookies: undefined /* BOOLEAN */,
proxyHost: undefined /* STRING */,
url: "https://<<serveraddresshere>>/0/devices/<<deviceidhere>>/s/t" /* STRING */,
timeout: undefined /* NUMBER */,
proxyPort: undefined /* INTEGER */,
password: undefined /* STRING */,
domain: undefined /* STRING */,
username: undefined /* STRING */
};

// result: JSON
var myJson = Resources["ContentLoaderFunctions"].GetJSON(params);

me.myProperty1 = myJson.value; // for your sample {"value":"42.42", "time":"1487606556741"}
me.myProperty2 = myJson.time;

But usually Json result contents more rows and for how to get a data in the jason, we need to know the structure of your json. Please check How to convert a "Json input" to an "Infotable" in a ThingWorx service

Lily