bvstone

Using the GETURI API to make a RESTful or Web Service Request

Posted:

Using the GETURI API to make a RESTful or Web Service Request

 

GETURI has been around for along time (over 15 years!), and has been used by thousands of companies for just about any purpose.  It is similar to the cURL command used on other platforms to make requests to web services which in turn return data.

But, what some don't realize is that GETURI has an API interface as well as a command interface.

The source below is a simple example of how to use the GETURI command to retrieve the current time calling a web service using the API interface, and following using the new ILE interface:

API Version:

      ****************************************************************
      /COPY GETURI/QCOPYSRC,GETURICOPY
      ****************************************************************
     D GetUriRG        pr                  extpgm('GETURIRG')    
     D   PR_In                             like(GetUri_In)                    
     D   PR_Out                            like(GetUri_Out)                   
     D   PR_Head                           like(GetUri_Head)                  
     D   PR_Data                           like(GetUri_Data)                  
     D   PR_MsgCd                          like(GetUri_MsgCd)                 
     D   PR_Msg                            like(GetUri_Msg)                   
      ****************************************************************
      /free
       EXSR $LoadParms;
       EXSR $GETURI;

       *INLR = *ON;
       //***************************************************************
       //* Call GETUI
       //***************************************************************
       BegSr $GETURI;

         GetUriRG(GetUri_In:GetUri_Out:GetUri_Head:GetUri_Data:
                  GetUri_MsgCd:GetUri_Msg);

       EndSr;
       //***************************************************************
       //* Load Parameters to call GETURIRG
       //***************************************************************
       BegSr $LoadParms;

         Clear GetUri_In;
         GI_URI = 'http://webservice.theknot.com/Time/GetTime.asmx/currentTime';
         GI_OutType = '*RETURN';
         GI_SprHead = '*YES';

       EndSr;
      /end-free

ILE Version:

**FREE
ctl-opt DFTACTGRP(*NO) ACTGRP('GETURI') BNDDIR('GETURI');

// Imports
/COPY QCOPYSRC,GETURICOPY
/COPY QCOPYSRC,P.GETURI

// Work Variables
dcl-s rc int(10);
dcl-s errMsg varchar(256);

geturi_resetValues();
geturi_setValue('gi_uri':'webservice.theknot.com/Time/GetTime.asmx/currentTime');
geturi_setValue('gi_outtype':'*RETURN');

rc = geturi_call(GetUri_Out:GetUri_Head:GetUri_MsgCd:GetUri_Msg:errMsg);

 

The important pieces to recognize with this are:

  1. The /COPY statement - This brings in all the variables and data structures needed to make a call to the GETURIRG API.  This /COPY book comes standard with the GETURI software.
  2. The CLEAR operation on the GetUri_In parameter - Because the variables used are in a data structure, using the CLEAR operation on the data structure will initialize each of the values to their value and we won't run into any decimal-data errors, or similar.
  3. The use of GI_OutType = '*RETURN' - What this does is return the response from the web request to the program, specifically to the GetUri_Out parameter
  4. The use of GI_SprHead = '*YES' - This tells GETURI to suppress the HTTP headers that are returned with the data.  This makes parsing the data much easier since we don't have to deal with the headers.  And, if you need to examine or use any of the header data, it will be available in the GetUri_Head variable.

If we run our program in debug, we can see the value that is returned:

                              Evaluate Expression 

 Previous debug expressions 

 > EVAL geturi_out                                                              
   GETURI_OUT =                                                                 
             ....5...10...15...20...25...30...35...40...45...50...55...60       
        1   '<?xml version="1.0" encoding="utf-8"?>  <string xmlns="http:'      
       61   '//webservice.theknot.com/Time">09/15/2014 10:48</string>    '      
      121   '                                                            '      
      181   '                                                            '      
      241   '                                                            '      
      301   '                                                            '      
      361   '                                                            '      
      421   '                                                            '      
      481   '                                                            '      
      541   '                                                            '      
      601   '                                                            '      
                                                                        More... 
 Debug . . . 

 F3=Exit   F9=Retrieve   F12=Cancel   F16=Repeat find   F19=Left   F20=Right 
 F21=Command entry       F23=Display output 

We see that the web service we're using returns XML containing the current time.

If we want to see the headers, we can simply evaluate GetUri_Head:

                              Evaluate Expression 

 Previous debug expressions 

 > EVAL geturi_head                                                             
   GETURI_HEAD =                                                                
             ....5...10...15...20...25...30...35...40...45...50...55...60       
        1   'HTTP/1.1 200 OK  Connection: keep-alive  Date: Mon, 15 Sep 2'      
       61   '014 15:48:34 GMT  Server: Microsoft-IIS/6.0  ServerID: AUSWE'      
      121   'B14  X-Powered-By: ASP.NET  Set-Cookie: temp=T20141509154834'      
      181   '524964; path=/; domain=theknot.com;expires=Fri 22-May-2020 1'      
      241   '3:00:00 GMT;  X-AspNet-Version: 2.0.50727  Cache-Control: pr'      
      301   'ivate, max-age=0  Content-Type: text/xml; charset=utf-8  Con'      
      361   'tent-Length: 116                                            '      
      421   '                                                            '      
      481   '                                                            '      
      541   '                                                            '      
      601   '                                                            '      
                                                                        More... 
 Debug . . . 

 F3=Exit   F9=Retrieve   F12=Cancel   F16=Repeat find   F19=Left   F20=Right 
 F21=Command entry       F23=Display output 

Because the information returned will be small, we can choose to return the data to our program.

The XML can then be parsed using your favorite XML parser.  

If the data returned would be quite large, it may be a better idea to return the data to a stream file instead of the program.  Then once it is retrieved you can pass the name of your stream file to your XML parser so it can do all the dirty work and get you some usable information.

 


Last edited 10/04/2022 at 10:43:11



Latest Posts:

QuickBooks Online - Subtotals and Discounts Frustration QuickBooks Online - Subtotals and Discounts Frustration
Posted by March 16, 2023
QuickBooks >> QuickBooks Online
Making the Switch From QuickBooks Desktop to QuickBooks Online - No Picnic Making the Switch From QuickBooks Desktop to QuickBooks Online - No Picnic
Posted by March 16, 2023
QuickBooks >> QuickBooks Online
BVSTools Software Verified on V7R5 and Power10 BVSTools Software Verified on V7R5 and Power10
Posted by December 9, 2022
BVSTools >> BVSTools Announcements
Microsoft Office 365 Servers and Random Errors Issue Microsoft Office 365 Servers and Random Errors Issue
Posted by November 14, 2022
BVSTools >> BVSTools Software Discussion >> Email Tools (MAILTOOL) Specific Discussion
Sending/Resending Emails Using a MIME File with MAILTOOL Sending/Resending Emails Using a MIME File with MAILTOOL
Posted by November 8, 2022
BVSTools >> BVSTools Software Discussion >> Email Tools (MAILTOOL) Specific Discussion
Sending an HTML Email on Your IBM i Using MAILTOOL Sending an HTML Email on Your IBM i Using MAILTOOL
Posted by November 1, 2022
BVSTools >> BVSTools Software Discussion >> Email Tools (MAILTOOL) Specific Discussion
Transferring License Keys from One System to Another Transferring License Keys from One System to Another
Posted by October 31, 2022
BVSTools >> BVSTools Software Discussion
Calculating the Size of a File Before Base64 Encoding Calculating the Size of a File Before Base64 Encoding
Posted by August 13, 2022
Programming >> RPG Programming
GreenTools for Microsoft Apps (G4MS) v9.12 Now Includes Function to Send Emails using MIME File GreenTools for Microsoft Apps (G4MS) v9.12 Now Includes Function to Send Emails using MIME File
Posted by August 11, 2022
BVSTools >> BVSTools Announcements >> GreenTools for Microsoft Apps (G4MS) Specific Announcements
GreenTools for Google Apps (G4G) v15.20 Now Supports Shortcuts GreenTools for Google Apps (G4G) v15.20 Now Supports Shortcuts
Posted by August 6, 2022
BVSTools >> BVSTools Announcements >> GreenTools for G Suite (Google Apps) (G4G) Specific Announcements
GreenTools for Microsoft Apps (G4MS) Groups Admin Authority Instructions GreenTools for Microsoft Apps (G4MS) Groups Admin Authority Instructions
Posted by July 26, 2022
BVSTools >> BVSTools Software Discussion >> GreenTools for Microsoft Apps (G4MS) Specific Discussion
GreenTools for Microsoft Apps (G4MS) v9.10 Now Includes OneDrive Functions that Work With Groups/Shared Drives GreenTools for Microsoft Apps (G4MS) v9.10 Now Includes OneDrive Functions that Work With Groups/Shared Drives
Posted by July 19, 2022
BVSTools >> BVSTools Announcements >> GreenTools for Microsoft Apps (G4MS) Specific Announcements
GreenTools for Google Apps (G4G) v15.10 Now Includes Drive Functions that Work With Shared Drives GreenTools for Google Apps (G4G) v15.10 Now Includes Drive Functions that Work With Shared Drives
Posted by July 15, 2022
BVSTools >> BVSTools Announcements >> GreenTools for G Suite (Google Apps) (G4G) Specific Announcements
GreenTools for Microsoft Apps (G4MS) v9.00 Now Offers Functions to Bypass Registration Command and BVSTools Landing Page GreenTools for Microsoft Apps (G4MS) v9.00 Now Offers Functions to Bypass Registration Command and BVSTools Landing Page
Posted by July 4, 2022
BVSTools >> BVSTools Announcements >> GreenTools for Microsoft Apps (G4MS) Specific Announcements
What Objects Should I Omit from Replication to Ensure My License Keys Work on my HA/DR System? What Objects Should I Omit from Replication to Ensure My License Keys Work on my HA/DR System?
Posted by June 27, 2022
BVSTools >> BVSTools Software Discussion

Reply




© Copyright 1983-2020 BVSTools
GreenBoard(v3) Powered by the eRPG SDK, MAILTOOL Plus!, GreenTools for Google Apps, jQuery, jQuery UI, BlockUI, CKEditor and running on the IBM i (AKA AS/400, iSeries, System i).