1 2 Previous Next 26 Replies Latest reply on Mar 28, 2018 11:42 AM by Christopher.Bakken

    How to make REST API calls to third party websites

    jerinn Rookie

      Can anyone who has successfully implemented REST API calls to third party websites using web services connection provide screen shots and example code?

       

      Thanks.

        • 1. Re: How to make REST API calls to third party websites
          Pandor Apprentice

          Hi jerinn

           

          I however have made the call and the script returned the desired results; in this case,published announcements.

          As shown below

          Below is the script;

          <body>

              <script>

                  Ext.onReady(function () {

                      $("body").addClass("loading");

                      makeApiCall();

                  });

              </script>

             

          <script>    

              function makeApiCall() {

                  HEATAPI.Search.SearchBusinessObject("Announcement",

                      "c2e6a783-caca-4725-baf2-231ec62801f9",

                      ["Region","Subject","CreatedDateTime","EffectiveDate"],

                      "LastModDateTime",

                      "DESC",

                      100,

                      processResult);       

              }

              function processResult(data) {

                  $("body").removeClass("loading");

           

           

                  Ext.create('Ext.grid.Panel', {

                      title: 'Published Announcements',

                      store: {

                          fields: ['Region', 'Subject','CreatedDateTime', 'EffectiveDate'],

                          data : data.data

                      },

                      columns: [

                          { text: 'Region', dataIndex: 'Region'},

                          { text: 'Subject', dataIndex: 'Subject', flex: 1 },

                          { text: 'Created On:', dataIndex: 'CreatedDateTime', flex: 1 },

                          { text: 'Effective From:', dataIndex: 'EffectiveDate', flex: 1 }

                      ],

                      width: 900,

                      renderTo: Ext.get('BusinessObjects')

                  });

              }

          </script>          

          <div id="BusinessObjects"></div>

             

              <div id="loadingOverlay" class="modal"><!-- Place for loading icon -->

           

           

          </div></body></html>

           

          The issue that I have however is going ahead and actually calling and being able to show the resuts from the third party website(or intranet).

           

          Hope that takes you half way!

           

          Pandor

          • 2. Re: How to make REST API calls to third party websites
            BKallweit Apprentice

            Hi Jerinn,

             

            You would need to write a javascript around the ExecuteWebRequest function. For a start, have a look at the online help at Working with Web Service Scripting.

             

            The Rest Call would be something simple like

            var response = ExecuteWebRequest('GET', url, null, getJsonRequestSettings());

             

            with getJsonRequestSettings doing something like

            return {

                        Headers: {

                            'authorization': 'Basic ' + Credentials,

                            'Content-Type': 'application/json'

                        },

                        Cookies: Cookies,

                        SkipServerCertificateValidation: true,

                        UserName: null,

                        Password: null,

                        AllowAutoRedirect: false

                    };

            depending on your needs. To build the Credentials you need something that encodes username and password as Base64. Perhaps your web service does not even require Basic authentication.

             

            Hope that get's you started.

             

            Bernd

            • 3. Re: How to make REST API calls to third party websites
              Christopher.Bakken SupportEmployee

              Here is another example, directly from the help file. It's a bit more involved, but basically does the same thing as yours.

               

              HEAT Service Management Online Help

               

               

              // JavaScript object to wrap REST API(s)

               

              function CustomService(host, userName, password)

              {

              'use strict';

              // private members

              var Host = host;

              var UserName = userName;

              var Password = password;

              var Cookies = null;

              var SessionInfo = null;

              var getWWWFormUrlEncodedData = function (obj)

              {

              var encoded = '', separator = '';

              for (var key in obj)

              {

              var value = obj[key];

              var encodedKey = HeatUtility.HtmlEncode(key.toString());

              if (typeof value === 'undefined' || null == value)

              encoded += separator + encodedKey;

              else if (value instanceof Array)

              {

              for (var i = 0; i < value.length; i++)

              {

              encoded += separator + encodedKey + '=' + HeatUtility.HtmlEncode(value[i] + '');

              separator = '&';

              }

              }

              else

              encoded += separator + encodedKey + '=' + HeatUtility.HtmlEncode(value.toString());

              separator = '&';

              }

              return encoded;

              };

              var getRequestSettings = function ()

              {

              return {

              Headers: {},

              Cookies: Cookies,

              SkipServerCertificateValidation: true,

              UserName: null,

              Password: null,

              AllowAutoRedirect: false

              };

              };

              var getJsonRequestSettings = function ()

              {

              return {

              Headers: { 'Content-Type': 'application/json' },

              Cookies: Cookies,

              SkipServerCertificateValidation: true,

              UserName: null,

              Password: null,

              AllowAutoRedirect: false

              };

              };

              var isAuthenticated = function ()

              {

              return !!SessionInfo && !!SessionInfo.tenantID;

              };

              var login = function ()

              {

              var url = Host + '/patchweb/tenant/v1/login?' + getWWWFormUrlEncodedData({ username: UserName, password: Password });

              var response = ExecuteWebRequest('GET', url, null, getRequestSettings());

              if ((response != null) && (response.StatusCode == 200) && (response.ContentType == 'application/json'))

              {

              Cookies = response.Cookies;

              var sessionInfo = JSON.parse(response.Data);

              if (!!sessionInfo && !!sessionInfo.tenantID)

              {

              SessionInfo = sessionInfo;

              return true;

              }

              }

              return false;

              };

              var getAllEndPoints = function (groups)

              {

              if (!isAuthenticated())

              throw 'Either login failed or please login.';

              var url = Host + '/patchweb/patchmanagement/v1/' + SessionInfo.tenantID + '/endpointpatchstates?groups=' + HeatUtility.HtmlEncode(groups);

              var response = ExecuteWebRequest('GET', url, null, getRequestSettings());

              if ((response != null) && (response.StatusCode == 200) && (response.ContentType == 'application/json'))

              return JSON.parse(response.Data);

              console.error(response);

              };

              // public function

              this.isAuthenticated = isAuthenticated;

              this.login = login;

              this.getAllEndPoints = getAllEndPoints;

              }

              var service = new CustomService('https://www.example.com', 'userName', 'password');

              service.login();

              if (service.isAuthenticated()) {

              var endpoints = service.getAllEndPoints('default,excluded');

              console.debug(endpoints);

              }

              else

              console.error('Login failed for user userName.');

              • 4. Re: How to make REST API calls to third party websites
                jerinn Rookie

                It will be helpful if once can provide screen shots as there is no documentation on how the function is supposed to work.

                 

                Thanks.

                • 5. Re: How to make REST API calls to third party websites
                  BKallweit Apprentice

                  For a start, have a look at the online help at 'Working with Web Service Scripting' where you find a fairly detailed description of 'ExecuteWebRequest'. It is imho one of the better parts of the ISM documentation. There is additional information in 'Working with Web Service Script Integration' as well.

                  You need some understanding of the Rest service you want to use; use a test tool like Postman to find out how to build the url. There's already a sample script above showing you how to embed the Rest call in a script.

                  This is scripting, so a text editor becomes your friend.

                   

                   

                   

                   

                  • 6. Re: How to make REST API calls to third party websites
                    jerinn Rookie

                    Thanks Bernd. I am confused as to what I should enter for Service Reference, Web Service List, and Method List?

                    • 7. Re: How to make REST API calls to third party websites
                      Christopher.Bakken SupportEmployee

                      You don't have to put anything in there. That's to help build the code based on a known service.

                       

                      For instance, I can use this test service as an example:  http://www.webservicex.net/uszip.asmx

                       

                      You can then see the available services that it offers.

                       

                      It will then generate a snippet for you to use based on the input data.

                       

                      As you can see in the code, it's just referencing the values of the 'script reference' so you can really just put all your references directly in the code if you know it.

                      • 8. Re: How to make REST API calls to third party websites
                        BKallweit Apprentice

                        Hi Jerinn

                         

                        to add to Christopher's comment, his example shows a way to work with a SOAP based web service. Heat offers you support to investigate SOAP services and even generates code snippets for published methods for you. You wouldn't neccessarily need to use other tools, like SoapUI, for this purpose.

                         

                        You asked for Rest API calls, and there is no GUI support for that in Heat. You write a script yourself (find an example above; if you loaded the the TSS package, there are lots of scripts for Rest web services e.g. with the Jira integration) using ExecuteWebRequest to communicate with the Rest API.

                         

                        Sorry to confuse you even more.

                         

                         

                        Bernd

                        • 9. Re: How to make REST API calls to third party websites
                          jerinn Rookie

                          Hi Christopher,

                          I am seeing the below message when I run it. Can you explain how I need to run after adding the script?

                           

                           

                          Thanks,

                          Jer

                          • 10. Re: How to make REST API calls to third party websites
                            Christopher.Bakken SupportEmployee

                            Run now is only going to work if you have a service tied to the action.

                             

                            Otherwise you have to call if from the business object...

                             

                            CALL INTERNAL WEB SERVICE DIRECTLY FROM BUSINESS RULE WITHOUT USING TRIGGER, OR QUICK ACTION

                             

                             

                            In theory, you could also make an incoming webservice that takes some action, then just call it locally.

                            • 11. Re: How to make REST API calls to third party websites
                              jerinn Rookie

                              Hi Christopher,

                              Can you tell how I can test the javascript associated with the webservices I need to call with the executewebrequest() method?

                               

                              Thanks,

                              Jerin

                              • 12. Re: How to make REST API calls to third party websites
                                Christopher.Bakken SupportEmployee

                                I don't understand what you are asking.

                                 

                                 

                                To test a script, just run it. If you need to, output variables to the console to verify they are what you expect.

                                • 13. Re: How to make REST API calls to third party websites
                                  jerinn Rookie

                                  Are you using a third party testing tool to test the RESTful API? As you mentioned  we cannot run it from the web service connections, I am not aware if there is a different way to test it.

                                  • 14. Re: How to make REST API calls to third party websites
                                    Christopher.Bakken SupportEmployee

                                    "Otherwise you have to call it from the business object..."

                                     

                                     

                                    1 2 Previous Next