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

Community Tip - Did you get called away in the middle of writing a post? Don't worry you can find your unfinished post later in the Drafts section of your profile page. X

[POSTJson] model POSTJson

gbeaumont
7-Bedrock

[POSTJson] model POSTJson

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

9 REPLIES 9
PaiChung
22-Sapphire I
(To:gbeaumont)

"/* 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.

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

ttielebein
14-Alexandrite
(To:gbeaumont)

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

yes I used POSTMAN to verify the syntax of my call and it worked. Actually I just want to do a classic Oauth2 request with password type

PaiChung
22-Sapphire I
(To:gbeaumont)

Please try it with POSTText and see if that works or not.

Yes, I kept everything the same in my javascript, including the JSON payload for the POST but called PostText(params) instead of PostJSON.  Important to note, the script must explicitly set the header content-type to application/json despite passing as text.

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 */,  ....

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.

PaiChung
22-Sapphire I
(To:cpeterson1)

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

Announcements


Top Tags