RPG example: Talk to a Webservice and store reply XML or JSON in IFS


Jun 01

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’

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.


About the Author

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.

%d bloggers like this: