.st0{fill:#FFFFFF;}

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

 June 1, 2017

By  NickLitten

Lets look at an example RPG program talking to a webservice

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.

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:

RPG JSON LIBHTTP example
**FREE 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.

NickLitten


IBM i Software Developer, Digital Dad, AS400 Anarchist, RPG Modernizer, Shameless Trekkie, Belligerent Nerd, Englishman Abroad 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 remember: If at first you don't succeed then skydiving probably isn't a hobby you should look into.

Nick Litten

related posts:

  • Hello Nick,

    thanks for the above post. I tried it out to better understand how to call REST webservice from RPGLE but i get the ff: when i run it:

    (GSKit) Key database file was not found.
    (GSKit) An operation which is not valid for the current SSL session state
    was attempted.

    I debugged and it returned WWCOMM = SPP:*NULL in the http_url_get_raw()

    But when i access the restipsum website directly, it returns json format data:

    https://restipsum.com/api/schema/47d88c40-65a6-11e7-9444-9f46428cb666

    I was hoping you have some tips on how i can correct the error above?

    thanks!
    Joel

    • Hi Joel, it seems you got past the compile stage? I cannot for the life of me figure out how to get the code into a source member without SEU errors on the dcl-s and dcl-c statements. This is all a bit foreign to me (an old, old programmer). I added the omitted H and D specs where I guessed they were needed but this dcl-s and dcl-c issue I just cannot understand what’s wrong with it. Am I missing something? Would appreciate any help I can get. Thanks, David Dolphin.

  • I am trying to get your sample code into a source member with SEU. Unfortunately you didn’t include the H’s and D’s required for column 6. I managed to guess some of them that I know about but I cannot get past the dcl-s and the dcl-c statements. I get an SEU error “An assignment operator is expected with the EVAL operation. ” Is there something I am missing here using copy and paste? Sorry if it’s a dumb question but I am in alien territory here. Also, are there any /free statements meant to be there? Thanks.

    • RPG no longer needs /FREE or /END-FREE – it now assumes everything is FREEFORMAT and only reverts to old style if it see’s a ‘C’ in column 6 for control specs or a ‘F’ in column six for file specs etc etc

  • I managed to figure out what i was doing wrong with my copy and paste. The problem was with SEU not recognizing the free format statements. I ignored the SEU errors and got a clean compile. Now comes the fun part, making it work.

    • Add ‘**FREE’ as line 1 in position 1 (in SEU do a L99 in the line command area to make sure you are fully left.

      SEU will still throw syntax errors (because IBM discontinued SEU support a couple of years back – to encourage adoption of RDi as a replacement) but it will compile OK. šŸ˜‰

  • {"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}
    __CONFIG_colors_palette__{"active_palette":0,"config":{"colors":{"cff50":{"name":"Main Accent","parent":-1},"a344d":{"name":"Accent Transparent","parent":"cff50"}},"gradients":[]},"palettes":[{"name":"Default","value":{"colors":{"cff50":{"val":"var(--tcb-skin-color-0)"},"a344d":{"val":"rgba(46, 138, 229, 0.85)","hsl_parent_dependency":{"h":210,"l":0.54,"s":0.78}}},"gradients":[]},"original":{"colors":{"cff50":{"val":"rgb(0, 178, 255)","hsl":{"h":198,"s":1,"l":0.5}},"a344d":{"val":"rgba(0, 178, 255, 0.85)","hsl_parent_dependency":{"h":198,"s":1,"l":0.5}}},"gradients":[]}}]}__CONFIG_colors_palette__

    Get In Touch

    Iā€™m always looking for awesome input, feedback and critique!

    >