8 Replies Latest reply on Jan 11, 2016 3:26 AM by veronicamih RSS
    deannarob Newbie

    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?

      • ThingWorx API rest calls
        veronicamih Creator

         

        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&appKey=6cd84a3e-2c42-448f-950b-69af1b0fb3b8&x-thingworx-session=true&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>

         



          • ThingWorx API rest calls
            veronicamih Creator

            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.

             



              • Re: ThingWorx API rest calls
                lzhou Apprentice

                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.

                  • Re: ThingWorx API rest calls
                    veronicamih Creator

                    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

              • Re: ThingWorx API rest calls
                sv Apprentice

                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?

                  • Re: ThingWorx API rest calls
                    veronicamih Creator

                    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

                      • Re: ThingWorx API rest calls
                        sv Apprentice

                        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

                          • Re: ThingWorx API rest calls
                            veronicamih Creator

                            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