Skip to main content
1-Visitor
March 9, 2020
Solved

API call PUT JSON content array instead of object

  • March 9, 2020
  • 2 replies
  • 2184 views

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

Best answer by Rick-Stanley

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);

 

 

 

2 replies

1-Visitor
March 9, 2020

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.

5-Regular Member
March 9, 2020

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);

 

 

 

micky_b1-VisitorAuthor
1-Visitor
March 16, 2020

This worked perfectly! Thanks a lot