Community Tip - Did you know you can set a signature that will be added to all your posts? Set it here! X
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
Solved! Go to Solution.
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);
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