As an RPG Programmer you are probably writing a ton of code to talk to remote webservices out there in internet land.
If you are just learning webservice programming, it can seem like a daunting exercise. So, to help you with your first steps on the journey, here is a simple RPG CODE EXAMPLE which talks to a remote webservice (using the open source HTTP API) and stores the reply from the webservice in the integrated File System.
The reply will typically be in XML or JSON in IFS, in this example I am storing JSON and then easily use the EDTF command to view it and see what it looks like.
I am using a cool website called RESTIPSUM to generate some JSON. This will create a temporary working URL ENDPOINT that will serve up some JSON. Of course, in this code example, you could change the webserviceURL to any webservice you wish:
ctl-opt dftactgrp(*no) actgrp('NICKLITTEN') option(*nodebugio:*srcstmt:*nounref) bnddir('HTTPAPI') alwnull(*inputonly) datfmt(*ISO) decedit('0.') copyright('GETWEBJSN 2017.06.30 Nick Litten | Example of simple- consumption of webservice returning JSON into IFS'); /include libhttp/qrpglesrc,HTTPAPI_H dcl-s rc int(10); // define URL of the webservice we want to consume dcl-c logFile const('/littenn/debugLog-GETWEBJSN.RPGLE.txt'); // define URL of the webservice we want to consume dcl-c webserviceURL const('https://restipsum.com/api/schema/47d88c40-65a6-11e7-9444-9f46428cb666'); // Define IFS location to store the reply in XML/JSON format // obviously you can change this to be a folder/file of your choice dcl-c webserviceReplyIFS const('/littenn/getwebjsn.json'); /title [ --- MAINLINE --- ] // Turn on HTTPAPI logging to write a Debug log out to the IFS. http_debug(*on:logFile); // GET request from 'URL' and place response in IFS at 'ifsWebData'. rc = http_url_get(webserviceURL:webserviceReplyIFS); *inlr = *on; return;
So, in this example, the RPGLE code connects to ‘https://restipsum.com/api/schema/47d88c40-65a6-11e7-9444-9f46428cb666’ and stores the JSON response data in ‘/littenn/getwebjsn.json’
NOTE: this is using the HTTPAPI (LIB:LIBHTTP) to perform the webservice call. So you will need to add lib(LIBHTTP) to your library list before compiling and running.
IBM i Software Developer, Digital Dad, AS400 Anarchist, RPG Modernizer, Alpha Nerd and Passionate Eater of Cheese and Biscuits. Nick Litten Dot Com is a mixture of blog posts that can be sometimes serious, frequently playful and probably down-right pointless all in the space of a day. Enjoy your stay, feel free to comment and in the words of the most interesting man in the world: Stay thirsty my friend.
RPG Evolution, Modernization and Refactoring – Step 5 of 4 – RPG FREE FORMAT 2017
Dirty RPG programmers need to write Clean RPG Code
IBM i create JOBLOG from RPGLE /FREE – a QMHSNDPM code example
Write to joblog messages from RPGLE /FREE – easy code example using Qp0zLprintf
Write RPG joblog messages – simple code example
How to add the Turnover V100 Perspective to WDSC7
Example RPGLE web service program [SOAP]
DSPF Function Keys and the Hex AID Byte from the INFDS
How to speed up IBM WDSC or RDi for faster RPG editing