6 Replies Latest reply on Aug 20, 2014 11:37 AM by Powell

    Looking for example of calling Jive REST from node.js

    Powell

      Hey,

      Getting my feet wet with node.js, wondering if any kind soul might have a simple example of calling a Jive REST API endpoint from node?

       

      Thank you!

      Powell

        • Re: Looking for example of calling Jive REST from node.js
          Ryan Rutan

          There are tons of ways to do it, but I would look at the Jive SDK ... as it is a treasure trove of examples IMO =)

           

          the method to look at would be:

          jive.util.buildRequest

          and its implemented here:

          https://github.com/jivesoftware/jive-sdk/blob/master/jive-sdk-api/lib/util/jiveutil.js#L98

          and subsequently here:

          jive-sdk/request.js at master · jivesoftware/jive-sdk · GitHub

          See the calls to it from here:

          Search Results · GitHub

           

          Hope that helps. But if you are using the Jive SDK for Node (Getting Started with the Jive Node SDK) then it should be as simple as:

          var headers = {

                          'Content-Type' : 'application/json'

                      };

          var data = { value = 123 }

          var options = { theseAreMiscValues = toChangeBehavior }

          var url = 'http://your.url';

          jive.util.buildRequest(url,'GET', data, headers, options).then(

               function(response) {

                    /** INVOKED IF SUCCESSFUL **/

               },

               function (error) {

                    /** INVOKED IF ERROR OCCURRED **/

               }

          );


          Hope that helps. =)

          • Re: Looking for example of calling Jive REST from node.js
            pawans

            I have some more simpler rest calls in my node.js service.

            Along with jive-sdk, I had created a new node.js service which is called by jive add-on and then this service makes a simpler jive REST api call.

            Here are some simple node.js code snippets which will help you to make any rest api call, not just jive.

            Changed the naming convention to make it more readable in jive editor. You can try to translate this simple code into any language.

             

            I created two methods to generate a GET and POST request as follows

             

                var createGetRequest = function(url){

                    var getRequest = {

                        'authz': "signed",

                        url: url,

                        method:'GET',

                        'auth': {

                            "user": "",

                            "pass": ""

                    }

                    };

                    return getRequest;

                }

             

             

                var createPostRequest = function(url,data){

                    var postRequest = {

                        'authz': "signed",

                        url: url,

                        method:'POST',

                        'auth': {

                            "user": "",

                            "pass": ""

                            },

                        body:data

                    };

                    return postRequest;

                }

             

             

            Now I can call these methods to generate respective rest calls whenever needed

             

            Example for GET to retrieve user info from username

            people_service_url = "https://myjiveurl/api/core/v3/people/username/" + username

             

            jiveRequest = createGetRequest(people_service_url)

             

            Then using the request module in node (var request = require('request');)

            request(jiveRequest, function(error, response,body){

            ......

            });

             

             

            similarly for post, example of adding user as member

            people_service_url  = "https://myjiveurl/api/core/v3/people/" + userId"

            data = '{"person":"' + people_service_url + '","state" : "member"}'

             

            group_member_Service_url = ""https://myjiveurl/api/core/v3/members/places/" + groupId""

            jivePostRequest  = createPostRequest(group_member_Service_url, data)

            request(jivePostRequest, function(error, response,body){

            ....

            } );

             

            I am using this service to do some extra magic which might not be possible for the jive javascript apis in add-ons.

            So I use jive connects api in add-ons to call my node.js service which has code similar to what I have put above.

            • Re: Looking for example of calling Jive REST from node.js
              aron.racho

              Here is another interesting approach: https://github.com/jivesoftware/GitHub4Jive/blob/master/GitHub4Jive-Addon/common/JiveApiFacade.js

               

              We are building up a client library for jive in the github4jive project which could be interesting for you to participate in if you are interested in seeing how a nontrivial integration service can be built in node using the jive sdk.

              • Re: Looking for example of calling Jive REST from node.js
                Powell

                Thank you Pawan, Ryan and Aron for responding.

                 

                I am close here (maybe ).  Trying just simple call before I dive into the SDK ..

                I think I am missing something fundamental

                Trying the below code gives an error =

                Error: read ECONNRESET

                    at errnoException (net.js:904:11)

                    at TCP.onread (net.js:558:19)

                Which makes me think it is hitting the jive instance and immediately being disconnected,,,

                Any help greatly appreciated!!!

                 

                -Thank you
                Powell

                 

                 

                 

                var request = require('request'),
                    username = "pt76511",
                    password = "MYPASSWORD",
                    url = "https://MYJIVEURL/api/core/v3/people/username/pt76511",
                    auth = "Basic " + new Buffer(username + ":" + password).toString("base64");

                request(
                    {
                        url : url,
                        headers : {
                            "Authorization" : auth
                        }
                    },
                    function (error, response, body) {
                       
                   if (!error && response.statusCode == 200) {
                       console.log(body)
                   }
                   if(error){
                    console.log('Opps error:'+error.stack)
                   }
                 
                    }
                );