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

Community Tip - Did you get an answer that solved your problem? Please mark it as an Accepted Solution so others with the same problem can find the answer easily. X

Can a REST API service request return a simple value?

asinclair
1-Newbie

Can a REST API service request return a simple value?

Our edge devices already implement a REST API comprising a set of HTTP service requests (POST only). Many of the service requests just push data but a few are expecting values to be returned. In the Composer, when I create a service that returns a single value (result), the HTTP response to the device is always an InfoTable with a single row and single column (in JSON format in this case). The value is the one I want, but now I have to change the firmware in every device to find the value in the response. Is there anyway to override the default behavior in this special case of services returning single values as an InfoTable? What are my options?

7 REPLIES 7

Hi Andy,

the way you get closest to what you need is the following:

change the following two headers of your REST request call:

Accept : text/csv

Content-Type : text/csv

To get a correctly formatted result with a system >=6.5 you'll have to change a setting in the Platform Subsystem: Switch off Filter Content Type'. This has some security impact, see Updating the Request Method and Content Type Filtering for CSRF Protection in the 6.6 HelpCenter.

The result will return your String in the second line (the first will be the header column. i.e. the name of the property, by default this is result.

I just tried these settings and invoked a getStringValue service on a TestThing Thing that returns a String. The invocation URL is:
http://192.168.159.131:8080/Thingworx/Things/TestThing/Services/getStringVal

and the result will then be:

"result"

"Hello World!"

Here is s screenshot of my Postman request:

postman_svc_csv.png

Cheers, moritz

I believe you are doing proper POST calls so you won't have to turn on the switch.

For the service return values, you can first of all define in the Service what the result parameter basetype is, so could even just be a number or string or boolean.

then with the Accept header you can  get text, text/xml, text/csv, application/json returned

Thanks, Moritz and Pai. Close is good - thanks for the suggestion on using text/csv. However, this solution will still require a small change in the edge device firmware.


I guess the real question is, when connecting with edge devices with a predefined API, Is it possible to customize the corresponding Thing services in a way that would allow me to, for example, control the exact encoding of the result in the HTTP response or use certain values from the HTTP URL and header?


Or, said another way, how does one produce a Thingworx extension to talk to a proprietary or non-standard device?

Thanks,

PaiChung
22-Sapphire I
(To:asinclair)

What response is your call expecting to be returned?

In one specific case, the device says it is expecting application/json of a dateTime offset value, but it is not expecting the InfoTable metadata that my service sends with the response. But there are other cases where the return value is different and I am looking at this as a general problem. These service also need to read values from the HTTP POST URL and/or header. Whatever the existing API is I need to develop services that match..

PaiChung
22-Sapphire I
(To:asinclair)

Hi Andy, if you want to use the Calls your equipment is making right now, there will be some major changes needed on the platform, as I indicated, Thingworx REST API can't consume the additional pieces you have in the URL.

So all the information will have to go to the Parameter declaration.

However when it comes to return values, again you can create services to create any BaseType information within Thingworx that you'll be calling and those can come back in text / xml / json or csv

Without a doubt some work will be involved on both ends.

I already successfully added jsp pages to the TWX codebase - these can then use TWX APIs and you'll have the freedom to format the output in any way you want. You could also create your own servlets, then you'll need modifications to the web.xml. 

Top Tags