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

Community Tip - Need help navigating or using the PTC Community? Contact the community team. X

postJSON error with thingworx

LaurentGermain
10-Marble

postJSON error with thingworx

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",

 



ACCEPTED SOLUTION

Accepted Solutions

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"

View solution in original post

3 REPLIES 3

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>]}

Thx, unfortunately, it produces the same error.

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"

Announcements


Top Tags