cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

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

SOLVED
Highlighted
Contributor

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

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

Tags (2)
1 ACCEPTED SOLUTION

Accepted Solutions

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

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 12

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

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

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

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.

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

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

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

Thanks Lily,

This looks like it might work for me in my context. I will give it a try and then mark the answer correct if it works for me. With curl I first issue a POST command to create a session cookie for the remote server and then after that the ordinary GET works to return the JSON. I may have to experiment with this a little. Perhaps I need to define withCookies as TRUE. The interaction with the remote server also works well inside of postman. I just need to package this into property setting Javascript. What you provide here looks promising.

Jim

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

You will also want to use the PostJSON service, instead of GetJSON, for an http POST request. You can find this in the snippets library of the service, under ContentLoaderFunctions.

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

There is also some great content on the ThingWorx Developer Portal for REST API's.

  1. REST API Cheatsheet
  2. REST API Quickstart
  3. REST API How To's

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

James,

Any update on this? Was Li Li Liu's post helpful? If so, could you click on the "correct answer" or "mark as helpful" button and let us know?

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

Jeremy,

I plan to try to implement this suggestion in the next couple of days. I got distracted by other projects.

If things work, I will mark it as the correct answer.

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

Thanks for the suggestions so far.

I am stuck now on getting JSON back from  my GET request.

As James McCuen suggested, I used the PostJSON command to set up the connection because I need to get a session cookie for the GET request to be honored. I save the cookie as a property value of my thing.

Then I create a Cookie header from the saved cookie value but the GetJSON command as invoked in my "GET" service does not produce the real JSON that I see when I use CURL or PostMan outside of the ThingWorx environment. Here is what my Get service looks like:

var headers = {"Cookie":me.thsqCookie};

var params = {

    headers: headers /* JSON */,

// withCookies: true /* BOOLEAN */,

  url: "https://0ac48bf3-9fab-4bad-8455-e394808eda6b.beta.thsq.io/0/devices/6eb52907-e63f-417e-9503-e76604db..." /* STRING */,

    domain: ".0ac48bf3-9fab-4bad-8455-e394808eda6b.beta.thsq.io"

};

// result: JSON

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

me.rawJSON = result;

//me.lastTemperature = result.value;

What gets returned as the result is

{"headers":{"Cookie":"session=s%3AJG9-E9hEXJNSR0iB5Z2BgmRqNt7KeN5C.5nE6FZmPFeVfNh%2Fb0wOo5AMjg8%2FjsDl0s5LpLU%2BjNGQ"}}

What I expect to see is:

{"value":"22.46","time":1488907300134}

I hope I am clear in my explanation. Suggestions appreciated.