Skip to main content
1-Visitor
October 5, 2016
Question

Rest API - Accessing POST body in ThingWorx Services

  • October 5, 2016
  • 2 replies
  • 3611 views

The question of accessing the full POST body has not been addressed yet in any of the documentation/community posts.

With the following destination:

https://.../Thingworx/Things/testthing/Services/TestRest?appKey=<appKey>&Content-Type=application/json&method=post

and Body:

{"Value1":11,"Value2":22,"Value3":33}


In the 'TestRest' service, I can logger.warn the body values if the TestRest service has inputs named 'Value1','Value2' and 'Value3'.

But if I don't know what those parameters are, how do I simply access the full message body that was sent?

2 replies

1-Visitor
October 5, 2016

Hi Matt,

I would try to pass the whole body in CSV format (in your case: "11,22,33") and then parse this CSV in Javascript inside the "TestRest" service.

Let me know if this helps?

mattl11-VisitorAuthor
1-Visitor
October 5, 2016

Unfortunately the body must be in json format

1-Visitor
October 5, 2016

Hi Matt Lang

In the 'TestRest' service add an input parameter of JSON type. Set this input parameter to equal to the var variable. Now iterate this var variable to access key and values of your message.

for example,

say In 'TestRest' service name of input parameter is JSONInput, i.e. of json type.

now write script as following:

var my_json = JSONInput;

for (var k in my_json) {

  logger.warn("key : " + k + " - value : " + my_json);

}

Please let me know if it is unclear and I can try to clarify it for you!

Thanks,

Meenakshi

mattl11-VisitorAuthor
1-Visitor
October 5, 2016

Hi Meenakshi, it looks like the JSONInput comes in as 'undefined' with that code. I still have the body set to {"Value1":11,"Value2":22,"Value3":33}
I'm using Hurl.it to test the REST requests.

1.png

2.png