Skip to main content
1-Visitor
October 25, 2016
Question

[POSTJson] model POSTJson

  • October 25, 2016
  • 2 replies
  • 7670 views

Hi guys !

I'm trying to join an extern API with Thingworx. I format my request with POSTMAN and now I want to use the snippet service PostJSON.

But I always had syntax issues with it.

var params = {

  proxyScheme: undefined /* STRING */,

  headers: undefined /* JSON */,

  ignoreSSLErrors: undefined /* BOOLEAN */,

  useNTLM: undefined /* BOOLEAN */,

  workstation: undefined /* STRING */,

  useProxy: undefined /* BOOLEAN */,

  withCookies: undefined /* BOOLEAN */,

  proxyHost: undefined /* STRING */,

  url: "https://api.netatmo.com/oauth2/token" /* STRING */,

  content: {

"grant_type":"password"

"client_id":"[YOUR_CLIENT_ID]"

"client_secret":"[YOUR_CLIENT_SECRET]"

"username":"[USERNAME]"

"password":"[PASSWORD]"

"/* JSON */,

  timeout: undefined /* NUMBER */,

  proxyPort: undefined /* INTEGER */,

  password: undefined /* STRING */,

  domain: undefined /* STRING */,

  username: undefined /* STRING */

};

// result: JSON

var result = Resources["ContentLoaderFunctions"].PostJSON(params);

Can you help me about how correctly use the syntax ?

Regards,

Guillaume

2 replies

22-Sapphire I
October 26, 2016

"/* JSON */,

  timeout: undefined /* NUMBER */,

  proxyPort: undefined /* INTEGER */,

  password: undefined /* STRING */,

  domain: undefined /* STRING */,

  username: undefined /* STRING */

};

seems to have " but no closing one, these items also seem placed rather strange.

gbeaumont1-VisitorAuthor
1-Visitor
October 26, 2016

Finally, I changed my code for :

var contents={

    'Content-Type': 'multipart/form-data',

    'grant_type':'password',

    'client_id':'[YOUR_CLIENT_ID]',

    'client_secret':'[YOUR_CLIENT_SECRET]',

    'username':'[USERNAME]',

    'password':'[PASSWORD]'

};

   

   

   

var params = {

  proxyScheme: undefined /* STRING */,

  headers: undefined /* JSON */,

  ignoreSSLErrors: undefined /* BOOLEAN */,

  useNTLM: undefined /* BOOLEAN */,

  workstation: undefined /* STRING */,

  useProxy: undefined /* BOOLEAN */,

  withCookies: undefined /* BOOLEAN */,

  proxyHost: undefined /* STRING */,

  url: 'https://api.netatmo.com/oauth2/token' /* STRING */,

  content: contents /* JSON */,

  timeout: undefined /* NUMBER */,

  proxyPort: undefined /* INTEGER */,

  password: undefined /* STRING */,

  domain: undefined /* STRING */,

  username: undefined /* STRING */

};

// result: JSON

var resultJson = Resources["ContentLoaderFunctions"].PostJSON(params);

var result = JSON.stringify(resultJson);

No more errors but when I test my service in TW the result is {"headers":"","error":"invalid_request"}

I don't understand why

5-Regular Member
October 26, 2016

Does the rest call work outside of ThingWorx, like have you verified the syntax of the actual API call in a browser?

1-Visitor
September 25, 2017

I too have the same issue.  I used a packet trace application to see what was being sent to the REST server...

Postman sends just the content json that I specify in the body:

[{ "id": "Mytag.value", "v": "112"}]

however, the ContentLoaderFunctions for PostJSON sends :

{"array":[{"v":99,"id":"Mytag.value"}]}

When I change the body of my Postman call to match the Thingworx call, I get the same Invalid Request error from the REST server.  The params for the post is written as:

var params = {

//proxyScheme: undefined /* STRING */,

headers: headers /* JSON */,

ignoreSSLErrors: 1 /* BOOLEAN */,

//useNTLM: 1 /* BOOLEAN */,

//workstation: undefined /* STRING */,

useProxy: 0 /* BOOLEAN */,

withCookies: 0 /* BOOLEAN */,

//proxyHost: undefined /* STRING */,

url:  'http://192.168.248.128:39320/iotgateway/write' /* STRING */,

content:[{ "id":tagname, "v":input}] /* JSON */,  ....

1-Visitor
September 27, 2017

This works - using PostText() to pass a JSON to Kepware IoT Gateway REST server:

var headers = {"authorization": me.MyServic(),

                         "content-type": "application/json",

                         "Accept":"*/*"            

                        }

var data = JSON.stringify([

  {

    "id":tagname,

    "v": input

  }

]);                          

var params = {

headers: headers /* JSON */,

ignoreSSLErrors: 1 /* BOOLEAN */,

useNTLM: 1 /* BOOLEAN */,

useProxy: 0 /* BOOLEAN */,

withCookies: 0 /* BOOLEAN */,

url:'https://192.168.248.140:39320/iotgateway/write' /* STRING */,

content: data /* JSON */,

timeout: 3000 /* NUMBER */

};

//  result: JSON

var result = Resources["ContentLoaderFunctions"].PostText(params);

* Set "content-type" to "application/json"

** Configure content data as a JSON

*** Use PostText() function.

22-Sapphire I
September 27, 2017

Great after that you can convert the response back to JSON I think ParseJSON should work? and work with the response as an object.