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

Community Tip - Learn all about PTC Community Badges. Engage with PTC and see how many you can earn! X

How to use a custom service with JSON input via POST

njourdan
1-Newbie

How to use a custom service with JSON input via POST

Hello everybody,

I created a custom service with a JSON Input called "Data". I am trying to test this service via Postman.

Data will contain something like this:

[

{

"var1":5,

"var2":10

},

{

"var1":5,

"var2":10

}

]

Can someone tell me how i correctly pass this to Thingworx? So how do i have to format my body in postman? And in Thingworx Javascript do I have to parse the json first or is it already passed as i specify the input as json.

Thanks. I think the documentation on these things is really sparse

1 ACCEPTED SOLUTION

Accepted Solutions
jkaczynski
4-Participant
(To:njourdan)

Hi Nicolas Jourdan​,

tl;dr

To make it easy - if you need to pass an array in a format of JSON, just add a JSON type input parameter e.g. "data".

This array can be accessed by refering to "array" property, e.g.:


data.array.length;

data.array[0];

data.array[0].var1;

data.array[1].var2;


To pass that data from Postman or other tool, you need to pass as a body of the POST request:


{

     "data": [

          {

               "var1": 1,

               "var2": 2

          },

          {

               "var1": 3,

               "var2": 4

          }

     ]

}


Please, remember to set a Header Content-Type: application/json and authorize with Application Key. If you expect any response, you can also set a Header Accept: application/json (if this format fits you).


Hope this helps.

Regards,

J.

View solution in original post

4 REPLIES 4
ankigupta
5-Regular Member
(To:njourdan)

Please check following article for details https://support.ptc.com/appserver/cs/view/solution.jsp?n=CS213601&lang=en_US

Added a screenshot here for reference:

jkaczynski
4-Participant
(To:njourdan)

Hi Nicolas Jourdan​,

tl;dr

To make it easy - if you need to pass an array in a format of JSON, just add a JSON type input parameter e.g. "data".

This array can be accessed by refering to "array" property, e.g.:


data.array.length;

data.array[0];

data.array[0].var1;

data.array[1].var2;


To pass that data from Postman or other tool, you need to pass as a body of the POST request:


{

     "data": [

          {

               "var1": 1,

               "var2": 2

          },

          {

               "var1": 3,

               "var2": 4

          }

     ]

}


Please, remember to set a Header Content-Type: application/json and authorize with Application Key. If you expect any response, you can also set a Header Accept: application/json (if this format fits you).


Hope this helps.

Regards,

J.

Thanks a lot its working now!! I also found the article posted by Ankit Gupta​ but it didnt work for me with the escaped quotation marks

jkaczynski
4-Participant
(To:njourdan)

Hello Nicolas Jourdan​,

I'm glad I could help. If you found my answer correct and working, please consider to mark it as a "correct answer" to mark it as a reference for other users.

Regards,

J.

Top Tags