HTTPAPI comes in a library called LIBHTTP.
This lesson is using HTTPAPI to perform the external webservice call.
This is an opensource web communication tool that greatly simplifies connecting out to webservices and reading their data.
You will need to add lib(LIBHTTP) to your library list before compiling and running.
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 from any webservice will typically be in XML or JSON format. We can do what we want with that reply - process it, store it, share it.
In this example I am retrieving JSON from my github server and then storing it in an IFS location.
We can 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:
CONSUME WEBSERVICE AND STORE JSON REPLY IN IFS
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://nicklitten.com/sample.json');
// Define IFS location to store the reply in XML/JSON format
// obviously you can change this to be any folder/file
dcl-c WEBSERVICEREPLYIFS const('/home/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 'WEBSERVICEREPLYIFS'.
rc = http_url_get(WEBSERVICEURL:WEBSERVICEREPLYIFS);
return;
So, in this example, the RPGLE code connects to my website at ‘https://nicklitten.com/sample.json’, uses HTTPAPI to retrieve the JSON and stores the JSON response data in the IBM i IFS at ‘/home/littenn/getwebjsn.json’
!
What next?
In this example we are storing the reply that we get back from the internet webservice. It's just a string of JSON and has yet to be processed, decoded and used.
We could of DECODED that JSON and processed it using JSON_TABLE or YAJL – but we will get that later.