IBM Support

IWS REST Web Services consume sample

Question & Answer


Question

RPG sample client that uses the APIs to perform a REST request

Answer

This link opens an article on IBM i IWS Web Services:

Send and receive user-defined SOAP and REST messages from RPG

This DCF document provides an updated sample RPG program discussed in the above article.

The sample program found here has several corrections that were required.


Details on how to compile and use this sample are in the link.


**free
ctl-opt pgminfo(*pcml:*module:*dclcase) DFTNAME(CLIENTR);
// ********************************************************************
// *
// IBM Web Services Client for ILE *
// *
// FILE NAME: client.RPGLE *
// *
// DESCRIPTION: Source to do REST request using transport APIs *
// *
// ********************************************************************
// LICENSE AND DISCLAIMER *
// ---------------------- *
// This material contains IBM copyrighted sample programming source *
// code ( Sample Code ). *
// IBM grants you a nonexclusive license to compile, link, execute, *
// display, reproduce, distribute and prepare derivative works of *
// this Sample Code. The Sample Code has not been thoroughly *
// tested under all conditions. IBM, therefore, does not guarantee *
// or imply its reliability, serviceability, or function. IBM *
// provides no program services for the Sample Code. *
// *
// All Sample Code contained herein is provided to you "AS IS" *
// without any warranties of any kind. THE IMPLIED WARRANTIES OF *
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND *
// NON-INFRINGMENT ARE EXPRESSLY DISCLAIMED. *
// SOME JURISDICTIONS DO NOT ALLOW THE EXCLUSION OF IMPLIED *
// WARRANTIES, SO THE ABOVE EXCLUSIONS MAY NOT APPLY TO YOU. IN NO *
// EVENT WILL IBM BE LIABLE TO ANY PARTY FOR ANY DIRECT, INDIRECT, *
// SPECIAL OR OTHER CONSEQUENTIAL DAMAGES FOR ANY USE OF THE SAMPLE *
// CODE INCLUDING, WITHOUT LIMITATION, ANY LOST PROFITS, BUSINESS *
// INTERRUPTION, LOSS OF PROGRAMS OR OTHER DATA ON YOUR INFORMATION *
// HANDLING SYSTEM OR OTHERWISE, EVEN IF WE ARE EXPRESSLY ADVISED OF *
// THE POSSIBILITY OF SUCH DAMAGES. *
// *
// <START_COPYRIGHT> *
// *
// Licensed Materials - Property of IBM *
// *
// 5770-SS1 *
// *
// (c) Copyright IBM Corp. 2016, 2016 *
// All Rights Reserved *
// *
// U.S. Government Users Restricted Rights - use, *
// duplication or disclosure restricted by GSA *
// ADP Schedule Contract with IBM Corp. *
// *
// Status: Version 1 Release 0 *
// <END_COPYRIGHT> *
// *
// Change log: Sept 2017 *
// uri needed to be null terminated. *
// Reset the transport object to its initial state *
// The DB field names were upper case. Changed to match the case *
// for the fields as present in DB file *
// ********************************************************************

/COPY /QIBM/ProdData/OS/WebServices/V1/client/include/Axis.rpgleinc

DCL-S rc INT(10);
DCL-S tHandle POINTER;

DCL-S uri CHAR(200);
DCL-S response CHAR(32768);
DCL-S request CHAR(32768);
DCL-S propBuf CHAR(100);
DCL-S propBuf2 CHAR(100);

// --------------------------------------------------------------------
// Web service logic. The code will attempt to invoke a Web service.
// --------------------------------------------------------------------

// Uncomment to enable trace
axiscAxisStartTrace('/tmp/axistransport.log': *NULL);

// Set URI in order to delete student
uri = 'http://rchpw720.rchland.ibm.com:10065/web/services/students/823M934LA'+ x'00';

// Create HTTP transport handle.
tHandle = axiscTransportCreate(uri:AXISC_PROTOCOL_HTTP11);
if (tHandle = *NULL);
PRINT ('TransportCreate() failed');
return;
endif;

// Delete student registration, ID is part of URI
PRINT ('==Deleting student registration record');

propBuf = 'DELETE' + X'00';
axiscTransportSetProperty(tHandle: AXISC_PROPERTY_HTTP_METHOD: %addr(propBuf));
flushAndReceiveData();

// Now create a new student registration
PRINT ('==Creating student registration record');

uri = 'http://rchpw720.rchland.ibm.com:10065/web/services/students'+ x'00';
axiscTransportReset(tHandle:uri); //Reset the transport object to its initial state

propBuf = 'Content-type' + X'00';
propBuf2 = 'application/json' + X'00';
axiscTransportSetProperty(tHandle: AXISC_PROPERTY_HTTP_HEADER:
%addr(propBuf):%addr(propBuf2));

propBuf = 'POST' + X'00';
axiscTransportSetProperty(tHandle: AXISC_PROPERTY_HTTP_METHOD: %addr(propBuf));

request = '{"studentID":"123456789","firstName":'
+ '"New","lastName":"Rec","gender":"Male"}';

rc = axiscTransportSend(tHandle: %ADDR(request): %len(%trim(request)): 0);
if (rc = -1);
checkError ('TransportSend()');
else;
flushAndReceiveData();
endif;


// Now retrieve all student records
axiscTransportReset(tHandle:uri); // Reset the transport object to its initial state
PRINT ('==Retrieving student registration records');
propBuf = 'GET' + X'00';
axiscTransportSetProperty(tHandle: AXISC_PROPERTY_HTTP_METHOD: %addr(propBuf));
flushAndReceiveData();

// Cleanup handle.
axiscTransportDestroy(tHandle);

*INLR=*ON;


// =========================================
// Print to standard out
// =========================================
DCL-PROC PRINT ;
dcl-pi *n;
msg varchar(5000) const;
end-pi;

dcl-pr printf extproc(*dclcase);
template pointer value options(*string);
dummy int(10) value options(*nopass);
end-pr;

dcl-c NEWLINE CONST(x'15');

printf(%TRIM(msg) + NEWLINE);
END-PROC PRINT;

// =========================================
// Handle error
// =========================================
DCL-PROC checkError ;
dcl-pi *n;
msg varchar(5000) const;
end-pi;

DCL-S axisCode INT(10);
DCL-S statusCode POINTER;
DCL-S rc INT(10);

axisCode = axiscTransportGetLastErrorCode(tHandle);
PRINT (msg + ' call failed: ' +
%CHAR(axisCode) + ':' +
%STR(axiscTransportGetLastError(tHandle)));

if (axisCode = EXC_TRANSPORT_HTTP_EXCEPTION);
rc = axiscTransportGetProperty(tHandle:
AXISC_PROPERTY_HTTP_STATUS_CODE: %ADDR(statusCode));
PRINT ('HTTP Status code: ' + %STR(statusCode));
endif;
END-PROC checkError;

// =========================================
// Flush and Receive data
// =========================================
DCL-PROC flushAndReceiveData;
dcl-pi *n;
end-pi;

DCL-S header POINTER;
DCL-S property CHAR(100);
DCL-S bytesRead INT(10) inz(0);

clear response;
clear header;

// Flush data so request is sent
rc = axiscTransportFlush(tHandle);
if (rc = -1);
checkError ('TransportFlush()');
return;
endif;

// Receive data and print out data and response to stdout
rc = axiscTransportReceive(tHandle: %ADDR(response): %SIZE(response): 0);
if (rc = 0);
PRINT ('No data to read');
else;
dow rc > 0 AND bytesRead < %SIZE(response);
bytesRead = bytesRead + rc;
rc = axiscTransportReceive(tHandle:
%ADDR(response)+bytesRead:
%SIZE(response)-bytesRead:
0);
enddo;
endif;

if (rc = -1);
checkError ('TransportReceive()');
elseif (bytesRead > 0);
PRINT ('Bytes read: ' + %CHAR(bytesRead));
PRINT ('Data: ' + response);
endif;

if (rc > -1);
rc = axiscTransportGetProperty(tHandle:
AXISC_PROPERTY_HTTP_STATUS_CODE:
%addr(header));
if (rc = -1);
checkError ('TransportGetProperty()');
else;
PRINT ('HTTP status code: ' + %str(header));
endif;
endif;

END-PROC flushAndReceiveData;



Download the source sample zip file here: CLIENTR

[{"Product":{"code":"SWG60","label":"IBM i"},"Business Unit":{"code":"BU058","label":"IBM Infrastructure w\/TPS"},"Component":"Web technologies","Platform":[{"code":"PF012","label":"IBM i"}],"Version":"Version Independent","Edition":"All Editions","Line of Business":{"code":"LOB57","label":"Power"}}]

Document Information

Modified date:
31 October 2023

UID

nas8N1022250