Skip to main content
1-Visitor
January 29, 2016
Question

Can a REST API service request return a simple value?

  • January 29, 2016
  • 7 replies
  • 12045 views

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

12-Amethyst
February 1, 2016

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

22-Sapphire I
February 1, 2016

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

asinclair1-VisitorAuthor
1-Visitor
February 1, 2016

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,