RPG example: store webservice reply XML or JSON in IFS

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 leaning webservice programming – 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 webserice 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:


RPG JSON LIBHTTP examplectl-opt dftactgrp(*no) actgrp('NICKLITTEN')
 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.

 // GET request from 'URL' and place response in IFS at 'ifsWebData'.
 rc = http_url_get(webserviceURL:webserviceReplyIFS);

 *inlr = *on;

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’

Then we can run two DECODE routines – one using JSON_TABLE and one using YAJL – and comparing timing and simplicity of use.

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.