Webservice RPG Code Example – read from internet webservice XML/SOAP

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.

HTTPAPI comes in a library called LIBHTTP. This is an opensource web communication tool that greatly simplifies connecting out to webservices and reading their data.

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.

When you connect to a webservice and read its response that is called consuming a webservice:


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

// This is a numeric response code that will be used to record the web-call status.

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');

// 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;

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.


Come back later!

I'm busy recording a video lesson for this course. Please check back later. I'm doing it as fast as a can.... honest!

{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}