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 the Community Ranking System, a fun gamification element of the PTC Community. X

API call PUT JSON content array instead of object

micky_b
3-Visitor

API call PUT JSON content array instead of object

Hi all,

 

I am creating a service which calls an external API using the Put JSON content loader function. I am having a problem with the content part of the params in the function where Thingworx expects me to send a JSON object but the API expects to receive an array of objects.

 

What I want to do:

 

 

 

var params = {
    headers: {headers_here},
    url: url_here
    //Thingworx expects content to be wrapped in {} but I need it to be []
    content: [{object: here}, {object: here}, {object: here}]
};

 

 

 

I don't get any errors on Thingworx but it doesn't change anything on the external API side. It works when sending through Postman so I suspect Thingworx is doing something behind the scenes to change the format of the data. 

 

Any help would be brilliant

1 ACCEPTED SOLUTION

Accepted Solutions

It does appear that PutJSON wraps a bare array inside an element named "array"

You can use PutText with a contentType header of "application/json" to send a bare array.

Notice this website postman-echo.com/put that reflects back whatever you send it is very helpful for debugging

 

 

var params = {
    url: "https://postman-echo.com/put",
	content: [{"val1": "firstVal"}, {"val2": "secondVal"}, {"val3": "thirdVal"}],
    contentType: "application/json"
};

// result: STRING
var result = Resources["ContentLoaderFunctions"].PutText(params);

 

 

 

View solution in original post

3 REPLIES 3
wposner-2
12-Amethyst
(To:micky_b)

You will most likely have to write a custom extension.  You mentioned you're not getting any response back from the API?  I had the exact same problem when trying to send a file using the PostBinary and PostMultipart.  The only way I was able to get things to work was through a custom Java extension that implemented the Post call exactly as the API I was calling was expecting things.

It does appear that PutJSON wraps a bare array inside an element named "array"

You can use PutText with a contentType header of "application/json" to send a bare array.

Notice this website postman-echo.com/put that reflects back whatever you send it is very helpful for debugging

 

 

var params = {
    url: "https://postman-echo.com/put",
	content: [{"val1": "firstVal"}, {"val2": "secondVal"}, {"val3": "thirdVal"}],
    contentType: "application/json"
};

// result: STRING
var result = Resources["ContentLoaderFunctions"].PutText(params);

 

 

 

This worked perfectly! Thanks a lot 

Top Tags