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

Community Tip - When posting, your subject should be specific and summarize your question. Here are some additional tips on asking a great question. X

Rest API - Accessing POST body in ThingWorx Services

mattl1
7-Bedrock

Rest API - Accessing POST body in ThingWorx Services

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?

4 REPLIES 4
ptc-5082906
12-Amethyst
(To:mattl1)

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?

Unfortunately the body must be in json format

magrawal
2-Explorer
(To:mattl1)

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

mattl1
7-Bedrock
(To:magrawal)

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

Announcements


Top Tags