9 Replies Latest reply on Sep 27, 2017 10:39 AM by pchung RSS
    gbeaumont Explorer

    [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

      • Re: [POSTJson] model POSTJson
        pchung Collaborator

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

          • Re: [POSTJson] model POSTJson
            gbeaumont Explorer

            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

          • Re: [POSTJson] model POSTJson
            cpeterson Newbie

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

              • Re: [POSTJson] model POSTJson
                cpeterson Newbie

                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.