Skip to main content
12-Amethyst
September 27, 2024
Solved

postJSON error with thingworx

  • September 27, 2024
  • 2 replies
  • 1051 views

Hi Team

I have an issue a with a PostJSON request.
I have tested succesfully the content below with PostMan.

When invoking this a POSTJSON request, seems it can not handle orrectrly the content data.

 

 

 

 

 

 

 

 

 

 var c=[
 {
 'login':'temp',
 'SensorData':[
 {
 "Timestamp": 1727265323000,
 "Quantity": "Temperature",
 "Value": "19.2",
 "Unit": "°C",
 "DataType": "Decimal",
 "ValueOK": true
 }
 ]
 }
 ] ;

 

 

 

let params = {
	proxyScheme: "https",
	headers: {
 "Client": "xxx", 
 "Branch": "xxx",
 "Api-Key": "xxx",
 "version": "3.0"
	},
	url: "https://api.platform.twinzo.eu/v3/sensors/batch",
	content: c,
};

// result: JSON
let result = Resources["ContentLoaderFunctions"].PostJSON(params);

 


Any idea of why it fails ?

 "Message": "sensorContract.array:\n\tCannot deserialize the current JSON object (e.g. {\"name\":\"value\"}) into type 'System.Collections.Generic.IEnumerable`1[Core.Contract.V3.Sensors.SensorBatchContract]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.\r\nTo fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List<T>) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object.\r\nPath 'array', line 1, position 9.\n",
 "responseStatus": {
 "reasonPhrase": "Bad Request",

 



Best answer by LaurentGermain

Found a solution ( similar to  https://community.ptc.com/t5/ThingWorx-Developers/API-call-PUT-JSON-content-array-instead-of-object/m-p/653352#M43453) :

Using PostText instead of PostJSON, and adding in the header:
            contentType: "application/json"

2 replies

16-Pearl
September 28, 2024

could you try like this:

content: JSON.stringify(c)

TWX always has some special behavior with json-arrays, resulting in a wrapped array in a json object {array:[<originalarray>]}

12-Amethyst
September 28, 2024

Thx, unfortunately, it produces the same error.

LaurentGermain12-AmethystAuthorAnswer
12-Amethyst
September 28, 2024

Found a solution ( similar to  https://community.ptc.com/t5/ThingWorx-Developers/API-call-PUT-JSON-content-array-instead-of-object/m-p/653352#M43453) :

Using PostText instead of PostJSON, and adding in the header:
            contentType: "application/json"