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

Community Tip - Learn all about PTC Community Badges. Engage with PTC and see how many you can earn! X

ThingWorx API rest calls

ptc-6071588
5-Regular Member

ThingWorx API rest calls

I have been trying to send data to ThingWorx. To start I had created a Thing and a property to send a value to. I have a web application called www.markcheli.com/dummyPhonedata and am trying to send values from the Javascript in there to Thingworx.

I tried the following command:

$.ajax({
url: 'http://acadev1.cloud.thingworx.com/Thingworx/Things/Am2302Thing/Properties/Prop_Temperature/',
type: 'PUT',
data: 'value=1'

})

Using AJAX I am just trying to send Dummy data to the property "Prop_Temperature". On my phone this website

asks for permission to Thingworx using a login- after logging in the data does not send so the endpoint is never reached.

If I post to http://acadev1.cloud.thingworx.com/Thingworx/Things/Am2302Thing/Properties/Prop_Temperature ?method=put&value=4 it will send the value of

4 to the Thing property of Prop_Temperature.

Additionally, If I do not use the safari web browser on my phone it does not request permission to ThingWorx,thus it cannot even try to put this data on the website.

I tried using request to get access to ThingWorx:

http://54.164.99.104/Thingworx/Things/Am2302Thing/Properties/Prop_Temperature?appKey=8d2d147f-09cd-4df9-bb5e-d749060ce787&method=put&x-thingworx-session=true&value=2


Where 54.164.99.104 is the IP address of ThingWorx, AM2302Thing is our thing, and 8d2d147f-09cd-4df9-bb5e-d749060ce787 is our API key

Is there a different Rest call that will allow access to ThingWorx and send the value to a given property?

Also is there a problem with my PUT request that is not allowing to send the data to a given property?

9 REPLIES 9
vmihai
12-Amethyst
(To:ptc-6071588)

h3. Hi DeAnna,

<br>I am not sure how the Ajax put request should be, but try using also this attribute in the URL : thingworx-session=true. So a REST call with this attribute would be for example :  http://10.132.36.68:8080/Thingworx/Things/RPISensor2/Properties/Temp?method=put&amp;appKey=6cd84a3e-2c42-448f-950b-69af1b0fb3b8&amp;x-thingworx-session=true&amp;value=85.

It may be that when making the rest call other browser either than safari you were not asked for credentials because you already had an opened session on a Thingworx Instance on this browsers. Is that correct ?

Also make sure the app key is for an administrator or developer account and that the instance on which Thingworx is hosted is accessible from the phone. <br>

Let me know if that worked. 

Veronica<br>



vmihai
12-Amethyst
(To:vmihai)

Also make sure that the Thing you created on Thingworx has a  GenericThing Template and  not a RemoteThing one because this will make the properties read-only and won't allow you to set properties to them through Rest Calls only through the EMS methods.



lzhou-2
2-Explorer
(To:vmihai)

Hi Veronica:

I am trying to use URL http://localhost:8080/Thingworx/Things/TestDHTThing2/Properties/Temperature?appKey=2d8ae62b-4690-4078-98cc-b6db69c135ff&x-thingworx-session=true&method=put&value=22  in chrome browser to set thing's properties. It does not work, is it forbidden in Thingworx 6.0.1 ?  The thing is based on GenericThing template and properties are not read-only.

vmihai
12-Amethyst
(To:lzhou-2)

Hi,

Yes, calling rest calls in the browser search bar has been disabled starting with ThingWorx 6.0. You can enable them by following these steps :

1.       Click on Subsystems under SYSTEM within the left pane of Composer

2.            Click on PlatformSubsystem

3.            Click on Configuration

4.            Check the box next to Allow Request Method Switch to allow all REST API calls through a browser URL

5.            Uncheck Filter Content-Type

Starting with ThingWorx 6.0 is recommended that you use an HTTP request web plugin like POSTMAN to test the rest calls. 

Let me know if this fixes the issue.

Thank you,

Veronica

sv
1-Visitor
1-Visitor
(To:ptc-6071588)

Hi,

I want to know whether it is possible to pass multiple parameters in a URL. For eg: I have thing with 3 properties and i wanted to pass this properties in the URL and update it. How it is possible? Can anyone help me out?

vmihai
12-Amethyst
(To:sv)

Hi Supriya V,

All Things have a  SetPropertyValues service which allows you to set multiple properties at once. This service needs an infotable as inputThis is how the rest call for this service would look like for a Thing that has these properties: Temperature, Humidity, Luminosity and Location

http://yourThingWorx.cloud.thingworx.com/Thingworx/Things/YourThingName/Services/SetPropertyValues?method=post&appKey=yourAppKey&values={"dataShape":{"fieldDefinitions":{"Temperature":{"name":"Temperature","baseType":"NUMBER"},"Humidity":{"name":"Humidity","baseType":"NUMBER"},"Luminosity":{"name":"Luminosity","baseType":"NUMBER"},"Location":{"name":"Location","baseType":"LOCATION"}}},"rows":[{"Temperature":1.0,"Humidity":2.0,"Luminosity":3.0,"Location":{"latitude":4.0,"longitude":5.0,"elevation":0.0}}]}


You need to change what is in bold to make it work on your ThingWorx instance.

Note that the values for the properties are given in the rows array.

Please let me know if you need help in adjusting this rest call to fit your needs.


You can also create your own customized service that will set the properties that you want. Check out the tutorial for Weather app with Arduino Uno that uses this approach.


Best regards,


Veronica

sv
1-Visitor
1-Visitor
(To:vmihai)

Hi Veronica,

Thankyou for your help.

I tried doing so, But i got an error "Unable To Convert From java.lang.String to INFOTABLE" .What should I do ?

When this is done will this be affected in the property list of the thing?

What I have to do is,pass the values of the properties through the URL for the project that we are doing.When this is fired, It should get updated in the mashup and the property list of the thing in the composer.

Regards,

Supriya

vmihai
12-Amethyst
(To:sv)

Hi Supriya,

The properties need to already exist on your Thing and have the same type (Number, Location etc) as the one that you declared in the infotable of your Rest call. This SetPropertyValues service purpose is only to update your property values, it won't create properties for your Thing.

Best regards,

Veronica

rmakhija
7-Bedrock
(To:vmihai)

Hello @vmihai ,

 

I am trying the same thing and it doesn't work. There is no error returned, I get a 200 HTTP OK but the value doesn't change. I have tried with Remote as well as Generic thing, both gave me same results. Could you please help?

Also, the tuturial link is no longer valid, could you please provide a latest link if there is one.

Has it changed with Thingworx version 8.5.5?

Below is my request:

curl --location --request POST 'https://<HostName>/Thingworx/Things/<GenericThingName>/Services/SetPropertyValues' \
--header 'appkey: <AppKey>' \
--header 'Accept: application/json' \
--header 'Content-Type: application/json' \
--data-raw '{
    "dataShape": {
        "fieldDefinitions": {           
            "TestProp1": {
                "name": "TestProp1",
                "description": "",
                "baseType": "STRING"
            }
        }
    },
    "rows": [
        {
            "TestProp1": "TestValue123"
        }
    ]
}'

 

 

Announcements
Top Tags