[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RE: Consume a webservice with initial login



Hello Mr. Crawford

I am new to this as well, however; in an attempt to help, below I've done a copy/paste of an RPG program, that I've written, that uses a User ID and Password for basic authentication to a consume a web service. Of particular note you will want to look at the data areas named ##TRAFUSER (which in this example contains the User ID) and ##TRAFPASS (which in this example contains the Password). This example is consuming a web service that follows the REST (Representational State Transfer) architectural style. If you are trying to consume a SOAP (Simple Object Access Protocol), or some other type if web service, this example may not be useful. Good luck and I hope this helps.

Paul Reid. 

Beginning of copied source...

      /copy qcopysrc,XXR000LE                                                                       
     H BNDDIR('DSBNDDIR':'HTTPAPI':'OMBNDDIR')                                                      
      *--------------------------------------------------------------------------------------------*
      * DSRTRAFDTA    : Retrieve FleetLocate Traffic Data                           
      * Description       : Retrieve the 'Traffic Data' from FleetLocate's web services and write out   
      *                             : to our database.                                                                        
      * Created by        : Paul Reid.                                                                  
      * Creation date  : Nov 27, 2014                                                                
      * Documentation : https://api.us.spireon.com/doc/                                             
      *               : www.timestampconvert.com                                                    
      *               : FleetLocate's service follows REST architectural style.                      
      *               : REST: stands for Representational State Transfer.                           
      *               : URI: Uniform Resource Identifier.                                           
      *               : GMT: Greenwich Mean Time.                                                   
      *--------------------------------------------------------------------------------------------*
      * Prototype call to this program:                                                             
      *--------------------------------------------------------------------------------------------*
      *                                                                                             
     D DSRTRAFDTA      PR                  extpgm('DSRTRAFDTA')                                     
     D DSRTRAFDTA      PI                                                                           
      *                                                                                             
      *--------------------------------------------------------------------------------------------*
      * Prototypes:                                                                                 
      *--------------------------------------------------------------------------------------------*
      *                                                                                             
      * FleetLocate Traffic Data. Write new records to DSPTRAFDTA.                                  
      /include qcopysrc,DSTTRAFDT                                                                   
      *                                                                                             
      * Scott Klement's URI Encoder.                                                                
      /define WEBFORMS                                                                              
      *                                                                                             
      * Scott Klement's HTTPAPI.                                                                    
      /include qrpglesrc,HTTPAPI_H                                                                  
      *                                                                                             
      * QTRACS APIs.                                                                                
      /include qcopysrc,OMTQTRAPI                                                                   
      *                                                                                             
      * SDS - System Data Structure.                                                                
      /include qcopysrc,XXR001LE                                                                    
      *                                                                                             
      *--------------------------------------------------------------------------------------------*
      * Prototype external call to DSRWALTLOG - Walmart/Isotrak Trailer Tracking - Log Record.      
      *--------------------------------------------------------------------------------------------*
      *                                                                                            
     D DSRWALTLOG...                                                                                
     D                 PR                  extpgm('DSRWALTLOG')                                     
     D trafficDataDS...                                                                             
     D                                     likeDS(DSRMTRAFDT_trafficDataDS)                         
      *                                                                                             
      *--------------------------------------------------------------------------------------------*
      * Initial one-time processing.                                                                
      *--------------------------------------------------------------------------------------------*
      *                                                                                             
     D initialProcessing...                                                                         
     D                 PR                                                                           
      *                                                                                             
      *--------------------------------------------------------------------------------------------*
      * For parsing traffic data.                                                                   
      *--------------------------------------------------------------------------------------------*
      *                                                                                             
     D parseTrafficData...                                                                          
     D                 PR                                                                           
     D userData...                                                                                  
     D                                 *   value                                                    
     D nestingDepth...                                                                              
     D                               10I 0 value                                                    
     D elementName...                                                                               
     D                             1024A   varying const                                            
     D elementPath...                                                                               
     D                            24576A   varying const                                            
     D elementValue...                                                                              
     D                            65535A   varying const                                            
     D attributes...                                                                                
     D                                 *   dim(32767)                                               
     D                                     const options(*varsize)                                  
      *                                                                                             
      *--------------------------------------------------------------------------------------------*
      * External data areas:                                                                        
      *--------------------------------------------------------------------------------------------*
      *                                                                                             
      * Username for web service                                                                    
     D ##TRAFUSER...                                                                                
     D                 S             20A   dtaara(##TRAFUSER)                                       
      *                                                                                             
      * Password for web service.                                                                   
     D ##TRAFPASS...                                                                                
     D                 S             20A   dtaara(##TRAFPASS)                                       
      *                                                                                             
      * Limit the number of events (positions) returned.                                            
     D ##TRAFLIMT...                                                                                
     D                 S              4P 0 dtaara(##TRAFLIMT)                                       
      *                                                                                             
      * Start-at timestamp. Start retrieving data after                                             
      * this timestamp. This is updated every run.                                                  
      * Example Timestamp: 1416993119068 = 11/26/2014 @ 04:11:58                                    
      * See www.timestampconvert.com to convert back and forth.                                     
     D ##TRAFSTAT...                                                                                
     D                 S             13P 0 dtaara(##TRAFSTAT)                                       
      *                                                                                             
      * Controls whether or not this job runs.                                                      
      * If this data area = 'RUN' then the program runs.                                            
      * Otherwise; the programs ends without processing anything.                                   
     D ##TRAFCTRL...                                                                                
     D                 S              3A   dtaara(##TRAFCTRL)                                       
      *                                                                                             
      * Controls whether or not the debug dump is on or off.                                        
     D ##TRAFDBUG...                                                                                
     D                 S              3A   dtaara(##TRAFDBUG)                                       
      *                                                                                             
      *--------------------------------------------------------------------------------------------*
      * Data structure definitions:                                                                 
      *--------------------------------------------------------------------------------------------*
      *                                                                                             
      * Used to write out a new record to DSPTRAFDTA.                                               
     D trafficDataDS...                                                                             
     D                 DS                  likeDS(DSRMTRAFDT_trafficDataDS)                         
     D errorInfo...                                                                                 
     D                 DS                  likeDS(UTTERRDS_errorDS)                                 
     D                                                                                              
      *                                                                                             
      * Used to determine if we are in Daylight Savings Time, or not.                               
     D systemConfigDataDS...                                                                        
     D                 DS                  likeDS(OMRMQTRAPI_systemConfigDataDS)                    
     D                                                                                              
      *                                                                                             
      *--------------------------------------------------------------------------------------------*
      * Field definitions:                                                                          
      *--------------------------------------------------------------------------------------------*
      *                                                                                             
      * Creation date and time for new records.                                                     
     D creationDate...                                                                              
     D                 S               D   inz(*loval)                                              
     D creationTime...                                                                              
     D                 S               T   inz(*loval)                                              
      *                                                                                             
      * Used to determine if we are in Daylight Savings Time, or not.                               
     D dataBlockToReturn...                                                                         
     D                 S              6A   inz(*blanks)                                             
      *                                                                                             
      * Used to debug HTTPAPI procedures.                                                           
     D debugLog...                                                                                  
     D                 S           1000A   varying                                                  
      *                                                                                             
      * The date and time of the last record we received from the web service.                      
      * We store this in ##TRAFSTAT. It's were we'll start on the next run.                         
     D endAtTimestamp...                                                                            
     D                 S             13P 0 inz(*zero)                                               
      *                                                                                             
      * Use Scott Klement's API to "web encode" the URI.                                            
     D form...                                                                                      
     D                 S                   like(WEBFORM)                                            
      *                                                                                             
      * Used to covert GMT to local time.                                                           
     D gmtOffSetHours...                                                                            
     D                 S              1P 0 inz(*zero)                                               
      *                                                                                             
      * Return for Scott Klement's subprocedures.                                                   
     D rc...                                                                                        
     D                 S             10I 0                                                          
      *                                                                                             
      * Uniform Resource Identifier.                                                                
     D uri...                                                                                       
     D                 S            256A                                                            
      *                                                                                             
      *--------------------------------------------------------------------------------------------*
      * Mainline:                                                                                   
      *--------------------------------------------------------------------------------------------*
      *                                                                                             
      /free                                                                                         
                                                                                                    
          // Only run if the data area is set to RUN.                                               
          in ##TRAFCTRL;                                                                            
          if ##TRAFCTRL = 'RUN';                                                                    
                                                                                                    
             // Initial processing.                                                                 
             initialProcessing();                                                                   
                                                                                                    
             // Debug HTTPAPI procedures. Only if data area is set to ON.                           
             if ##TRAFDBUG = 'ON ';                                                                 
                debugLog = '/fleetLocate/fleetLocateDebugLog.txt';                                  
                http_debug(*on : debugLog);                                                         
             endif;                                                                                 
                                                                                                    
             // Set basic authentication.                                                           
             http_setAuth(HTTP_AUTH_BASIC :                                                         
                          %trim(##TRAFUSER) :                                                       
                          %trim(##TRAFPASS));                                                       
                                                                                                    
             // Encode the variables used in the URI.                                               
             form = webform_open();                                                                 
             webform_setVar(form :                                                                  
                           'limit' :                                                                
                            %trim(%char(##TRAFLIMT)));                                              
             webform_setVar(form :                                                                  
                           'startAt' :                                                              
                            %trim(%char(##TRAFSTAT)));                                              
             uri = 'https://api.spireon.com/api/asset/traffic.xml?'+                                
                    webform_getData(form);                                                          
             webform_close(form);                                                                   
                                                                                                    
             // 'Get' the data from the web service.                                                
             rc = http_url_get_xml(uri :                                                            
                                   *null :                                                          
                                   %paddr(parseTrafficData) :                                       
                                   *null);                                                          
                                                                                                    
             // If something went wrong.                                                            
             if rc <> 1;                                                                            
                http_crash();                                                                       
             else;                                                                                  
                                                                                                    
                // Record start-at timestamp for next run.                                          
                if endAtTimestamp <> *zero;                                                         
                   ##TRAFSTAT = endAtTimestamp;                                                     
                   out ##TRAFSTAT;                                                                  
                endif;                                                                              
             endif;                                                                                 
                                                                                                    
          endif;                                                                                    
                                                                                                    
          // The End.                                                                               
          *inlr = *on;                                                                              
          return;                                                                                   
                                                                                                    
      /end-free                                                                                     
      *                                                                                             
      *--------------------------------------------------------------------------------------------*
      * Initial processing:                                                                         
      *--------------------------------------------------------------------------------------------*
      *                                                                                             
     P initialProcessing...                                                                         
     P                 B                                                                            
      *                                                                                             
     D initialProcessing...                                                                         
     D                 PI                                                                           
      *                                                                                             
      /free                                                                                         
                                                                                                    
          // All records for a run will have the same creation date & time.                         
          creationDate = %date();                                                                   
          creationTime = %time();                                                                   
                                                                                                    
          // Username for the web service.                                                 
          in ##TRAFUSER;                                                                            
                                                                                                    
          // Password for the web service.                                              
          in ##TRAFPASS;                                                                            
                                                                                                    
          // Limit for number of events returned.                                            
          in ##TRAFLIMT;                                                                            
                                                                                                    
          // Find out if we want debug ON or OFF when we run.                                       
          in ##TRAFDBUG;                                                                            
                                                                                                    
          // Start At timestamp.                                                                    
          in *lock ##TRAFSTAT;                                                                      
                                                                                                    
          // Determine the GMT offset based on whether                                              
          // or not we are in Daylight Savings Time                                                 
          dataBlockToReturn = 'CFG200'; // Time Configuration.                                      
          systemConfigDataDS = OMRMQTRAPI_getSystemConfigData(                                      
                                          dataBlockToReturn);                                       
          if systemConfigDataDS.returnStatus = '0';                                                 
             if %subst(systemConfigDataDS.returnConfigData:5:1) = 'Y';                              
                gmtOffSetHours = 4;                                                                 
             else;                                                                                  
                gmtOffSetHours = 5;                                                                 
             endif;                                                                                 
          else;                                                                                     
             gmtOffSetHours = *zero;                                                                
          endif;                                                                                    
                                                                                                    
          // Clear data fields before populating them.                                              
          clear trafficDataDS;                                                                      
          endAtTimestamp = *zero;                                                                   
                                                                                                    
      /end-free                                                                                     
      *                                                                                             
     P initialProcessing...                                                                         
     P                 E                                                                            
      *                                                                                             
      *--------------------------------------------------------------------------------------------*
      * Parse the Traffic Data as it is received from FleetLocate.                                  
      *--------------------------------------------------------------------------------------------*
      *                                                                                             
     P parseTrafficData...                                                                          
     P                 B                                                                            
      *                                                                                             
     D parseTrafficData...                                                                          
     D                 PI                                                                           
     D userData...                                                                                  
     D                                 *   value                                                    
     D nestingDepth...                                                                              
     D                               10I 0 value                                                    
     D elementName...                                                                               
     D                             1024A   varying const                                            
     D elementPath...                                                                               
     D                            24576A   varying const                                            
     D elementValue...                                                                              
     D                            65535A   varying const                                            
     D attributes...                                                                                
     D                                 *   dim(32767)                                               
     D                                     const options(*varsize)                                  
      *                                                                                             
      * Local variables:                                                                            
     D gmtDateTime...                                                                               
     D                 S             19A    inz(*blank)                                             
     D localIsoDate...                                                                              
     D                 S               D    inz(*loval)                                             
     D localIsoTime...                                                                              
     D                 S               T    inz(*loval)                                             
      *                                                                                             
      * Convert GMT to Local Date and Time.                                                         
     D convertGMTtoLocalTime...                                                                     
     D                 PR                                                                           
     D gmtDateTime...                                                                               
     D                               19A   const                                                    
     D localIsoDate...                                                                              
     D                                 D                                                            
     D localIsoTime...                                                                              
     D                                 T                                                            
      *                                                                                             
      /free                                                                                         
                                                                                                    
          if elementName = 'map';                                                                   
                                                                                                    
              // Since more than one <map> element can be returned, check to                        
              // ensure that you have an actual Unique ID before writing out                        
              // a new record.                                                                      
              if trafficDataDS.tdTrailer# <> *blanks;                                               
                                                                                                    
                // Write a new Traffic Data record.                                                 
                trafficDataDS.tdCrtDate = creationDate;                                             
                trafficDataDS.tdCrtTime = creationTime;                                             
                DSRMTRAFDT_writeTrafficDataRecord(trafficDataDS :                                   
                                                  errorInfo);                                       
                                                                                                    
                // Log postion data.                                             
                DSRWALTLOG(trafficDataDS);                                                          
                                                                                                    
                // Clear data structure before parsing next record.                                 
                clear trafficDataDS;                                                                
             endif;                                                                                 
                                                                                                    
          // Trailer Number.                                                                        
          elseif elementName = 'name';                                                              
             trafficDataDS.tdTrailer# = %trim(elementValue);                                        
                                                                                                    
          // Event Date, Time & Type.                                                               
          elseif elementName = 'eventDateTime';                                                     
             gmtDateTime = *blanks;                                                                 
             gmtDateTime = %trim(elementValue);                                                     
             if gmtDateTime <> *blanks;                                                             
                convertGMTtoLocalTime(gmtDateTime :                                                 
                                      localIsoDate :                                                
                                      localIsoTime);                                                
                trafficDataDS.tdEvntDate = localIsoDate;                                            
                trafficDataDS.tdEvntTime = localIsoTime;                                            
             endif;                                                                                 
          elseif elementName = 'eventTypeName';                                                     
             trafficDataDS.tdEvntName = %trim(elementValue);                                        
                                                                                                    
          // Trailer's Address.                                                                     
          elseif elementName = 'address';                                                           
             trafficDataDS.tdAddress = %trim(elementValue);                                         
          elseif elementName = 'city';                                                              
             trafficDataDS.tdCity = %trim(elementValue);                                            
          elseif elementName = 'state';                                                             
             trafficDataDS.tdState = %trim(elementValue);                                           
          elseif elementName = 'zip';                                                               
             trafficDataDS.tdZip = %trim(elementValue);                                             
                                                                                                    
          // Trailer's Latitude & Longitude.                                                        
          elseif elementName = 'lat';                                                               
             monitor;                                                                               
                trafficDataDS.tdLatitude = %dec(%trim(elementValue):18:15);                         
             on-error;                                                                              
                trafficDataDS.tdLatitude = *zero;                                                   
             endmon;                                                                                
          elseif elementName = 'lng';                                                               
             monitor;                                                                               
                trafficDataDS.tdLongitud = %dec(%trim(elementValue):18:15);                         
             on-error;                                                                              
                trafficDataDS.tdLongitud = *zero;                                                   
             endmon;                                                                                
                                                                                                    
          // Is the trailer moving? When did it start moving?                                       
          elseif elementName = 'moving';                                                            
             trafficDataDS.tdIsMoving = %trim(elementValue);                                        
          elseif elementName = 'movingStartTime';                                                   
             gmtDateTime = *blanks;                                                                 
             gmtDateTime = %trim(elementValue);                                                     
             if gmtDateTime <> *blanks;                                                             
                convertGMTtoLocalTime(gmtDateTime :                                                 
                                      localIsoDate :                                                
                                      localIsoTime);                                                
                trafficDataDS.tdMoveDate = localIsoDate;                                            
                trafficDataDS.tdMoveTime = localIsoTime;                                            
             endif;                                                                                 
                                                                                                    
          // Is the trailer stopped? When did it stop?                                              
          elseif elementName = 'stopped';                                                           
             trafficDataDS.tdIsStoppd = %trim(elementValue);                                        
          elseif elementName = 'stoppedStartTime';                                                  
             gmtDateTime = *blanks;                                                                 
             gmtDateTime = %trim(elementValue);                                                     
             if gmtDateTime <> *blanks;                                                             
                convertGMTtoLocalTime(gmtDateTime :                                                 
                                      localIsoDate :                                                
                                      localIsoTime);                                                
                trafficDataDS.tdStopDate = localIsoDate;                                            
                trafficDataDS.tdStopTime = localIsoTime;                                            
             endif;                                                                                 
                                                                                                    
          // Direction the trailer is moving.                                                       
          elseif elementName = 'heading';                                                           
             monitor;                                                                               
                trafficDataDS.tdHeading = %dec(%trim(elementValue):3:0);                            
             on-error;                                                                              
                trafficDataDS.tdHeading = *zero;                                                    
             endmon;                                                                                
                                                                                                    
          // Speed of the trailer in KM/H.                                                          
          elseif elementName = 'speed';                                                             
             monitor;                                                                               
                trafficDataDS.tdSpeedKMH = %dec(elementValue:4:1);                                  
             on-error;                                                                              
                trafficDataDS.tdSpeedKMH = *zero;                                                   
             endmon;                                                                                
                                                                                                    
          // Distance tracked by GPS in Meters.                                                     
          elseif elementName = 'gpsTrackedDistance';                                                
             monitor;                                                                               
                trafficDataDS.tdGpsDistM = %dec(%trim(elementValue):30:15);                         
             on-error;                                                                              
                trafficDataDS.tdGpsDistM = *zero;                                                   
             endmon;                                                                                
                                                                                                    
          // Landmark ID & Name.                                                                    
          elseif elementName = 'landmarkId';                                                        
             trafficDataDS.tdLandMark = %trim(elementValue);                                        
          elseif elementName = 'landmarkName';                                                      
             trafficDataDS.tdLandName = %trim(elementValue);                                        
                                                                                                    
          // Trailer's Serial # and Device Id.                                                      
          elseif elementName = 'serial';                                                            
             trafficDataDS.tdSerial# = %trim(elementValue);                                         
          elseif elementName = 'deviceId';                                                          
             trafficDataDS.tdDeviceId = %trim(elementValue);                                        
                                                                                                    
          // Capture the endAt time stamp. We'll need this time stamp                               
          // to pass as the startAt time stamp for the next run.                                    
          elseif elementName = 'endAt';                                                             
             monitor;                                                                               
                endAtTimestamp = %dec(%trim(elementValue):15:0);                                    
             on-error;                                                                              
                endAtTimestamp = *zero;                                                             
             endmon;                                                                                
                                                                                                    
             // Record the timestamp on each record.                                                
             trafficDataDS.tdTimestmp = endAtTimestamp;                                             
                                                                                                    
          endif;                                                                                    
                                                                                                    
      /end-free                                                                                     
      *                                                                                             
     P parseTrafficData...                                                                          
     P                 E                                                                            
      *                                                                                             
      *--------------------------------------------------------------------------------------------*
      * Convert a GMT Date/Time Stamp to local in ISO format.                                       
      *--------------------------------------------------------------------------------------------*
      *                                                                                             
     P convertGMTtoLocalTime...                                                                     
     P                 B                   export                                                   
      *                                                                                             
     D convertGMTtoLocalTime...                                                                     
     D                 PI                                                                           
     D gmtDateTime...                                                                               
     D                               19A   const                                                    
     D localIsoDate...                                                                              
     D                                 D                                                            
     D localIsoTime...                                                                              
     D                                 T                                                            
      *                                                                                             
      * Local definitions:                                                                          
     D alphaDateTime...                                                                             
     D                 S             26A    inz(*blank)                                             
     D timeStamp...                                                                                 
     D                 S               Z    inz(*loval)                                             
      *                                                                                             
      /free                                                                                         
                                                                                                    
          // Re-arrange the GMT passed in from FleetLocate into an                                  
          // alpha field that exactly matches the ISO timestamp.                                    
          alphaDateTime = %subst(gmtDateTime:1:4) +                                                 
                          '-' +                                                                     
                          %subst(gmtDateTime:6:2) +                                                 
                          '-' +                                                                     
                          %subst(gmtDateTime:9:2) +                                                 
                          '-' +                                                                     
                          %subst(gmtDateTime:12:2) +                                                
                          '.' +                                                                     
                          %subst(gmtDateTime:15:2) +                                                
                          '.' +                                                                     
                          %subst(gmtDateTime:18:2) +                                                
                          '.' +                                                                     
                          '000000';                                                                 
                                                                                                    
          // Ensure timestamp is valid.                                                             
          test(ZE) *ISO alphaDateTime;                                                              
                                                                                                    
          // Populate ISO Date & Time.                                                              
          if not %error;                                                                            
             timeStamp = %timeStamp(alphaDateTime:*ISO)                                             
                                  - %hours(gmtOffSetHours);                                         
             localIsoDate = %date(%subst(%char(timeStamp):1:10));                                   
             localIsoTime = %time(%subst(%char(timeStamp):12:8));                                   
          else;                                                                                     
             localIsoDate = *loval;                                                                 
             localIsoTime = *loval;                                                                 
          endif;                                                                                    
                                                                                                    
          return;                                                                                   
                                                                                                    
      /end-free                                                                                     
      *                                                                                             
     P convertGMTtoLocalTime...                                                                     
     P                 E                                                                            
      *                                                                                             
      *--------------------------------------------------------------------------------------------*
      * End of source.                                                                              
      *--------------------------------------------------------------------------------------------*


------------------------------------------------------------------------------------------------------
Paul Reid
Application Developer III
Erb Group of Companies | 290 Hamilton Road | New Hamburg, Ontario | N3A 1A2
Phone: 519.662.6133 ext. 2363
Web: http://www.erbgroup.com/

-----Original Message-----
From: ftpapi-bounces@xxxxxxxxxxxxxxxxxxxxxx [mailto:ftpapi-bounces@xxxxxxxxxxxxxxxxxxxxxx] On Behalf Of KSCrawford
Sent: Tuesday, February 16, 2016 5:24 PM
To: ftpapi@xxxxxxxxxxxxxxxxxxxxxx
Subject: Consume a webservice with initial login

I have a web service I am needing to consume using an RPGLE program.This web service has an initial login.I am having trouble with this login.I have been trying    http_setauth   http_getauth   http_url_post_stmfin various orders etc.
When I talked to the people on the other side they say.I do see connections coming in secure, but they're all 401unauthorized.  My suspicion is you aren't supplying username and passwordas prvapp1 is expecting

Can someone help me understand what I need to do. Kerwin Crawford
-----------------------------------------------------------------------
This is the FTPAPI mailing list.  To unsubscribe, please go to:
http://www.scottklement.com/mailman/listinfo/ftpapi
-----------------------------------------------------------------------