<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Dodik Setyawan</title>
    <description>The latest articles on DEV Community by Dodik Setyawan (@dodikibmi).</description>
    <link>https://dev.to/dodikibmi</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4220799%2Febb085b5-0bb2-4e16-bf0c-041e78f923fa.jpg</url>
      <title>DEV Community: Dodik Setyawan</title>
      <link>https://dev.to/dodikibmi</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/dodikibmi"/>
    <language>en</language>
    <item>
      <title>Unit testing RPG with iRPGUnit: what to test, what a failure looks like, and how to run it in CI</title>
      <dc:creator>Dodik Setyawan</dc:creator>
      <pubDate>Mon, 09 Nov 2026 11:49:37 +0000</pubDate>
      <link>https://dev.to/dodikibmi/unit-testing-rpg-with-irpgunit-what-to-test-what-a-failure-looks-like-and-how-to-run-it-in-ci-1i13</link>
      <guid>https://dev.to/dodikibmi/unit-testing-rpg-with-irpgunit-what-to-test-what-a-failure-looks-like-and-how-to-run-it-in-ci-1i13</guid>
      <description>&lt;p&gt;Most RPG code I have seen is tested the same way: compile, call the program with a few records, look at the report or the screen, and compare by eye. That works until someone changes a rule six months later and nobody remembers which records proved it.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/tools-400/irpgunit" rel="noopener noreferrer"&gt;iRPGUnit&lt;/a&gt; gives RPG the same kind of unit tests other languages have had for years. This article shows a real suite of 20 tests, what the report looks like when everything passes, and, more useful, what it looks like when something is wrong. Everything below was run on &lt;a href="https://pub400.com" rel="noopener noreferrer"&gt;PUB400&lt;/a&gt; (IBM i 7.5, iRPGUnit v6.0.5) and the output is copied from those runs.&lt;/p&gt;

&lt;h2&gt;
  
  
  First, something you can test
&lt;/h2&gt;

&lt;p&gt;A unit test calls a procedure with known input and checks the result. That only works if the rule lives in a procedure that does not read a file, write a record or show a screen.&lt;/p&gt;

&lt;p&gt;In legacy code that is rarely the case. In my &lt;a href="https://github.com/dodikibmi/ibmi-modernization-showcase" rel="noopener noreferrer"&gt;modernization showcase&lt;/a&gt; the payment rules of an accounts receivable application were buried in the calculation specs of two programs, between &lt;code&gt;CHAIN&lt;/code&gt;s and indicators. Step 3 of the modernization moved them into a service program, &lt;code&gt;ARSRV&lt;/code&gt;, with six small procedures and no I/O:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Age bucket 1-5 for a number of days past due
dcl-proc AR_ageBucket export;
  dcl-pi *n int(10);
    daysPastDue int(10) const;
  end-pi;

  select;
    when daysPastDue &amp;lt;= 0;
      return AR_BUCKET_CURRENT;
    when daysPastDue &amp;lt;= 30;
      return AR_BUCKET_1_30;
    when daysPastDue &amp;lt;= 60;
      return AR_BUCKET_31_60;
    when daysPastDue &amp;lt;= 90;
      return AR_BUCKET_61_90;
    other;
      return AR_BUCKET_OVER_90;
  endsl;
end-proc;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The programs that read the files now call these procedures. The tests call them too, without any files at all. This is the part that takes the real work. Writing the tests afterwards is quick.&lt;/p&gt;

&lt;h2&gt;
  
  
  What a test suite looks like
&lt;/h2&gt;

&lt;p&gt;An iRPGUnit test suite is a &lt;code&gt;NOMAIN&lt;/code&gt; module. Every exported procedure whose name starts with &lt;code&gt;test&lt;/code&gt; is one test case. You include the iRPGUnit prototypes and the prototypes of the code under test:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;**FREE
ctl-opt nomain option(*srcstmt:*nodebugio);

/copy RPGUNIT/QINCLUDE,TESTCASE
/copy QRPGLESRC,ARSRVPR

dcl-proc test_validate_amount_checked_first export;
  // legacy order: an invalid amount wins over an unknown customer
  aEqual(AR_MSG_INVALID_AMOUNT : AR_validatePayment(0 : *off));
end-proc;

dcl-proc test_days_over_leap_day export;
  iEqual(2 : AR_daysPastDue(d'2024-03-01' : 20240228));
end-proc;

dcl-proc test_bucket_boundaries export;
  iEqual(AR_BUCKET_CURRENT : AR_ageBucket(-19));
  iEqual(AR_BUCKET_CURRENT : AR_ageBucket(0));
  iEqual(AR_BUCKET_1_30    : AR_ageBucket(1));
  iEqual(AR_BUCKET_1_30    : AR_ageBucket(30));
  iEqual(AR_BUCKET_31_60   : AR_ageBucket(31));
  iEqual(AR_BUCKET_31_60   : AR_ageBucket(60));
  iEqual(AR_BUCKET_61_90   : AR_ageBucket(61));
  iEqual(AR_BUCKET_61_90   : AR_ageBucket(90));
  iEqual(AR_BUCKET_OVER_90 : AR_ageBucket(91));
end-proc;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;(Replace &lt;code&gt;RPGUNIT&lt;/code&gt; with the library where iRPGUnit is installed.) Two commands do the rest:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;RUCRTRPG TSTPGM(ARSRVT) SRCFILE(QRPGLESRC) BNDSRVPGM(ARSRV)
RUCALLTST TSTPGM(ARSRVT) DETAIL(*ALL)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;RUCRTRPG&lt;/code&gt; compiles the module and creates the test service program bound to the code under test. &lt;code&gt;RUCALLTST&lt;/code&gt; runs every test procedure and writes a spooled file named &lt;code&gt;RPGUNIT&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The full suite has 20 test cases: every validation rule, every bucket boundary (0/1, 30/31, 60/61, 90/91), a leap day, a payment larger than the invoice, and one test for a defect the legacy program really had (more on that below). The last line of the report:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Success. 20 test cases, 28 assertions, 0 failure, 0 error.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  What a failure looks like
&lt;/h2&gt;

&lt;p&gt;A green report proves less than it seems. The useful question is: if someone breaks a rule, does a test notice?&lt;/p&gt;

&lt;p&gt;So I made a copy of the service program, &lt;code&gt;ARSRVX&lt;/code&gt;, with one character changed:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    when daysPastDue &amp;lt; 30;           // DELIBERATE DEFECT: should be &amp;lt;= 30
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;An invoice exactly 30 days overdue now lands in the 31-60 bucket. On a real aging report that moves money from one column to the next, and the totals still add up, so nobody sees it. I bound the same, unchanged test suite to the broken copy and ran it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;TEST_BUCKET_BOUNDARIES - FAILURE
Expected 2, but was 3.
  TEST_BUCKET_BOUNDARIES (ARSRVTX-&amp;gt;ARSRVTX:9400)
Expected:        1 '2'
Actual:          1 '3'
-----------------------
...
FAILURE. 20 test cases, 23 assertions, 1 failure, 0 error.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Three things in this report are worth knowing:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;It points at the line.&lt;/strong&gt; &lt;code&gt;9400&lt;/code&gt; is the source statement (94.00, thanks to &lt;code&gt;option(*srcstmt)&lt;/code&gt;) of the assertion that failed: &lt;code&gt;iEqual(AR_BUCKET_1_30 : AR_ageBucket(30))&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;A failed assertion ends the test case.&lt;/strong&gt; The suite had 28 assertions when it passed, and only 23 ran this time. The boundary test stopped at its fourth assertion, so the five after it never ran. If one test checks many things, you only learn about the first one that is wrong. That is a reason to keep test cases small, or at least to put the cheapest checks first.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The other 19 tests stayed green.&lt;/strong&gt; The failure names the rule that broke, not just "something changed".&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Failure, error, and an exception you expect
&lt;/h2&gt;

&lt;p&gt;iRPGUnit separates two kinds of red. To see both, I wrote a small demo suite (&lt;code&gt;RUDEMOT&lt;/code&gt;) with one of each:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// a wrong expectation on purpose: what a FAILURE looks like
dcl-proc test_failure_on_purpose export;
  aEqual('A' : AR_customerStatus('H' : 0.01) : 'status');
end-proc;

// an unexpected exception: what an ERROR looks like
dcl-proc test_error_on_purpose export;
  dcl-s zero int(10) inz(0);
  dcl-s x int(10);
  x = 100 / zero;
end-proc;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The report:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;TEST_ERROR_ON_PURPOSE - ERROR
MCH1211 - Attempt made to divide by zero for fixed point operation.
  TEST_ERROR_ON_PURPOSE (RUDEMOT-&amp;gt;RUDEMOT:6100)
-----------------------
TEST_FAILURE_ON_PURPOSE - FAILURE
status: Expected 'A', but was 'H'.
  TEST_FAILURE_ON_PURPOSE (RUDEMOT-&amp;gt;RUDEMOT:5300)
Expected:        1 'A'
Actual:          1 'H'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A &lt;strong&gt;failure&lt;/strong&gt; means the code ran and returned the wrong answer. An &lt;strong&gt;error&lt;/strong&gt; means the code crashed. The third parameter of &lt;code&gt;aEqual&lt;/code&gt; (&lt;code&gt;'status'&lt;/code&gt;) is a field name that is put in front of the message. With several assertions in one test it tells you which value was wrong without opening the source.&lt;/p&gt;

&lt;p&gt;Sometimes the exception &lt;em&gt;is&lt;/em&gt; the expected result. What does &lt;code&gt;AR_daysPastDue&lt;/code&gt; do with a due date of 2026-13-32 from bad data? It should not return a number. You can test that with &lt;code&gt;monitor&lt;/code&gt; and &lt;code&gt;getMonitoredMessage&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;dcl-proc test_bad_due_date_is_an_exception export;
  dcl-ds msg likeds(MsgInfo_t);
  dcl-s days int(10);
  monitor;
    days = AR_daysPastDue(d'2026-10-31' : 20261332);
    fail('AR_daysPastDue accepted 2026-13-32');
  on-error;
    msg = getMonitoredMessage(*on);
  endmon;
  aEqual('RNX0112' : msg.id);
end-proc;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This test passed: the procedure ends with &lt;code&gt;RNX0112&lt;/code&gt; (date, time or timestamp value is not valid). If someone later "fixes" the procedure so it quietly returns 0 for a bad date, the &lt;code&gt;fail()&lt;/code&gt; line turns this test red, and the team has to decide on purpose whether that is the behavior they want.&lt;/p&gt;

&lt;h2&gt;
  
  
  setUp, tearDown, and the order tests run in
&lt;/h2&gt;

&lt;p&gt;iRPGUnit calls four optional procedures if you export them: &lt;code&gt;setUpSuite&lt;/code&gt; and &lt;code&gt;tearDownSuite&lt;/code&gt; once around the whole suite, and &lt;code&gt;setUp&lt;/code&gt; and &lt;code&gt;tearDown&lt;/code&gt; around every test. They are the place to create test data, clear a work file, or reset a data area.&lt;/p&gt;

&lt;p&gt;I wanted to see the exact order, so each procedure in the demo suite appended one word to a data area. In the source, the tests are written in this order: &lt;code&gt;numeric&lt;/code&gt;, &lt;code&gt;failure&lt;/code&gt;, &lt;code&gt;error&lt;/code&gt;, &lt;code&gt;baddate&lt;/code&gt;. The data area after the run:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;setUpSuite setUp baddate tearDown setUp error tearDown setUp failure tearDown setUp numeric tearDown tearDownSuite
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Two things:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;tearDown&lt;/code&gt; ran after the failed test and after the crashed test too.&lt;/strong&gt; Cleanup in &lt;code&gt;tearDown&lt;/code&gt; is safe even when a test goes wrong. In the failing test, the line after the failed &lt;code&gt;aEqual&lt;/code&gt; never ran (there is no &lt;code&gt;after-failed-assert&lt;/code&gt; in the trace).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The tests ran in alphabetical order of their names, not in source order.&lt;/strong&gt; The report is sorted the same way. Do not write a test that depends on another test having run first. If two tests need the same data, create it in &lt;code&gt;setUp&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Small things that cost me time
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Comparing numbers as strings.&lt;/strong&gt; One of my first tests was:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;aEqual('.00' : %char(AR_applyAmount(0 : 500.00)));   // %char drops the leading zero
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It passes, but only because I wrote &lt;code&gt;'.00'&lt;/code&gt; after seeing what &lt;code&gt;%char&lt;/code&gt; returns. iRPGUnit 6 (the version on PUB400) also has an overloaded &lt;code&gt;assertEqual&lt;/code&gt; that compares numbers as numbers, which says what you mean. Both lines below passed in the demo suite:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;assertEqual(0 : AR_applyAmount(0 : 500.00));
assertEqual(300.00 : AR_applyAmount(300.00 : 500.00));
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Testing the bug you fixed.&lt;/strong&gt; The legacy report program printed &lt;code&gt;*UNKNOWN* (FICTIONAL)&lt;/code&gt; for a missing customer, because a &lt;code&gt;MOVEL&lt;/code&gt; of &lt;code&gt;*UNKNOWN*&lt;/code&gt; left the end of the previous customer's name in the field. The fix is one line. The test that keeps it fixed is just as short:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;dcl-proc test_unknown_customer_has_no_leftover_name export;
  aEqual('*UNKNOWN*' : %trimr(AR_customerName(*off : 'EKA FOODS (FICTIONAL)')));
end-proc;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Every defect you fix is a test case you already know you need.&lt;/p&gt;

&lt;h2&gt;
  
  
  Running the tests in CI
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;RUCALLTST&lt;/code&gt; ends with escape message &lt;code&gt;CPF9897&lt;/code&gt; when any test fails or errors (&lt;code&gt;FAILURE. 20 test cases, 23 assertions, 1 failure, 0 error.&lt;/code&gt;). Called through the &lt;code&gt;system&lt;/code&gt; command in PASE, that is a non-zero exit code, which is all a CI pipeline needs. In my &lt;a href="https://github.com/dodikibmi/ibmi-inventory" rel="noopener noreferrer"&gt;ibmi-inventory&lt;/a&gt; project, a GitHub Actions job builds the code from Git with IBM Bob (&lt;code&gt;makei&lt;/code&gt;) and then runs this over SSH on the IBM i:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;system &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$RULIB&lt;/span&gt;&lt;span class="s2"&gt;/RUCRTRPG TSTPGM(&lt;/span&gt;&lt;span class="nv"&gt;$OBJLIB&lt;/span&gt;&lt;span class="s2"&gt;/DATEUTILT) SRCSTMF('&lt;/span&gt;&lt;span class="nv"&gt;$PWD&lt;/span&gt;&lt;span class="s2"&gt;/qrpglesrc/dateutilt.rpgle') BNDSRVPGM((&lt;/span&gt;&lt;span class="nv"&gt;$OBJLIB&lt;/span&gt;&lt;span class="s2"&gt;/DATEUTIL)) TGTCCSID(*JOB)"&lt;/span&gt;
system &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$RULIB&lt;/span&gt;&lt;span class="s2"&gt;/RUCALLTST TSTPGM(&lt;/span&gt;&lt;span class="nv"&gt;$OBJLIB&lt;/span&gt;&lt;span class="s2"&gt;/DATEUTILT) DETAIL(*ALL)"&lt;/span&gt; 2&amp;gt;&amp;amp;1 | &lt;span class="nb"&gt;tee&lt;/span&gt; .logs/rpgunit.txt
&lt;span class="nb"&gt;grep&lt;/span&gt; &lt;span class="nt"&gt;-q&lt;/span&gt; &lt;span class="s2"&gt;"Success"&lt;/span&gt; .logs/rpgunit.txt
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The script runs with &lt;code&gt;set -euo pipefail&lt;/code&gt;, so the escape message from a failed run stops it even though the output goes through &lt;code&gt;tee&lt;/code&gt; (without &lt;code&gt;pipefail&lt;/code&gt;, the exit code of that line would be &lt;code&gt;tee&lt;/code&gt;'s, and a red test would pass the pipeline). The last &lt;code&gt;grep&lt;/code&gt; is a second check: the job is green only if the report really says &lt;code&gt;Success&lt;/code&gt;. The report goes into the job's artifacts, so the reviewer of a pull request sees which test went red without signing on to the IBM i.&lt;/p&gt;

&lt;h2&gt;
  
  
  The rules I follow
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Move the rule out of the I/O first.&lt;/strong&gt; A procedure with no files is easy to test. A program full of &lt;code&gt;CHAIN&lt;/code&gt;s is not.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Test the boundaries, not the middle.&lt;/strong&gt; 0, 1, 30, 31. The off-by-one in this article was caught only by the test for exactly 30.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Break the code on purpose once.&lt;/strong&gt; If no test goes red, the suite proves less than it seems.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Keep test cases small.&lt;/strong&gt; A failed assertion ends the test, so the checks after it tell you nothing.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Never depend on test order.&lt;/strong&gt; Shared data goes in &lt;code&gt;setUp&lt;/code&gt;, cleanup in &lt;code&gt;tearDown&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Every fixed defect gets a test.&lt;/strong&gt; It is the cheapest test to write and the one most likely to matter again.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Let the pipeline run them.&lt;/strong&gt; Tests that need a person to remember to run them stop being run.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The full suite, the service program and the regression outputs are in the &lt;a href="https://github.com/dodikibmi/ibmi-modernization-showcase" rel="noopener noreferrer"&gt;modernization showcase&lt;/a&gt; repository. If your shop runs RPG unit tests in a pipeline, I would like to hear how you handle tests that need database data: a test library per run, &lt;code&gt;setUp&lt;/code&gt; with SQL inserts, or something else.&lt;/p&gt;

</description>
      <category>rpg</category>
      <category>as400</category>
      <category>ibmi</category>
      <category>testing</category>
    </item>
    <item>
      <title>Service program signatures on IBM i: MCH4431, *PRV, and the change that breaks nothing loudly</title>
      <dc:creator>Dodik Setyawan</dc:creator>
      <pubDate>Sun, 08 Nov 2026 07:12:08 +0000</pubDate>
      <link>https://dev.to/dodikibmi/service-program-signatures-on-ibm-i-mch4431-prv-and-the-change-that-breaks-nothing-loudly-3fdn</link>
      <guid>https://dev.to/dodikibmi/service-program-signatures-on-ibm-i-mch4431-prv-and-the-change-that-breaks-nothing-loudly-3fdn</guid>
      <description>&lt;p&gt;Sooner or later every shop that uses service programs sees this in a job log:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;MCH4431  Program signature violation.
Cause . . . :  The source program SIGCALL specifies a signature
               X'00000000000000000000000CDE436702' which is not
               supported by service program SIGSRV.
Recovery  . :  The service program interface has changed.
               Re-bind source program SIGCALL.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The recovery text says "re-bind", and that is usually what people do: recompile the program that failed, then the next one, then the next one. It works, but it treats the symptom. The cause is how the service program was built.&lt;/p&gt;

&lt;p&gt;To see exactly what happens, I ran five small experiments on &lt;a href="https://pub400.com" rel="noopener noreferrer"&gt;PUB400&lt;/a&gt; (IBM i 7.5): one service program &lt;code&gt;SIGSRV&lt;/code&gt;, one caller &lt;code&gt;SIGCALL&lt;/code&gt;, and a data area where the caller writes what it got back. Every result below is copied from those runs.&lt;/p&gt;

&lt;h2&gt;
  
  
  The setup
&lt;/h2&gt;

&lt;p&gt;The service program starts with two procedures. Each one returns its own letter, so it is easy to see which procedure actually ran:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;       ctl-opt nomain;

       dcl-proc SigA export;
         dcl-pi *n char(10) end-pi;
         return 'A';
       end-proc;

       dcl-proc SigB export;
         dcl-pi *n char(10) end-pi;
         return 'B';
       end-proc;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The caller calls both and writes the answers to a data area:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;       dcl-pr SigA char(10) end-pr;
       dcl-pr SigB char(10) end-pr;

       dcl-s outTxt char(40) dtaara('SIGOUT');

       in *lock outTxt;
       outTxt = 'SigA=' + %trim(SigA()) + ' SigB=' + %trim(SigB());
       out outTxt;

       *inlr = *on;
       return;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Built the usual way:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;CRTRPGMOD MODULE(QTEMP/SIGSRV) SRCFILE(MYLIB/QRPGLESRC) SRCMBR(SIGSRV)
CRTSRVPGM SRVPGM(MYLIB/SIGSRV) MODULE(QTEMP/SIGSRV) EXPORT(*ALL)
CRTRPGMOD MODULE(QTEMP/SIGCALL) SRCFILE(MYLIB/QRPGLESRC) SRCMBR(SIGCALL)
CRTPGM    PGM(MYLIB/SIGCALL) MODULE(QTEMP/SIGCALL) +
          BNDSRVPGM((MYLIB/SIGSRV)) ACTGRP(*NEW)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Result: &lt;code&gt;SigA=A SigB=B&lt;/code&gt;. So far, nothing special.&lt;/p&gt;

&lt;h2&gt;
  
  
  What a signature is
&lt;/h2&gt;

&lt;p&gt;When &lt;code&gt;CRTPGM&lt;/code&gt; binds &lt;code&gt;SIGCALL&lt;/code&gt; to &lt;code&gt;SIGSRV&lt;/code&gt;, it stores two things in the program:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;the &lt;strong&gt;signature&lt;/strong&gt; of the service program at that moment (16 bytes), and&lt;/li&gt;
&lt;li&gt;for every procedure it uses, the &lt;strong&gt;position&lt;/strong&gt; of that procedure in the export list, not its name.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;At activation time the system checks the stored signature against the list of signatures the service program supports. If it is not in the list, you get &lt;code&gt;MCH4431&lt;/code&gt; before a single line of your program runs.&lt;/p&gt;

&lt;p&gt;Both values can be read with SQL. The caller side is in &lt;code&gt;QSYS2.BOUND_SRVPGM_INFO&lt;/code&gt;, and the service program side in &lt;code&gt;QSYS2.PROGRAM_INFO&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;select&lt;/span&gt; &lt;span class="n"&gt;hex&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;bound_service_program_signature&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;qsys2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;bound_srvpgm_info&lt;/span&gt;
 &lt;span class="k"&gt;where&lt;/span&gt; &lt;span class="n"&gt;program_library&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'MYLIB'&lt;/span&gt; &lt;span class="k"&gt;and&lt;/span&gt; &lt;span class="n"&gt;program_name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'SIGCALL'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="c1"&gt;-- 00000000000000000000000CDE436702&lt;/span&gt;

&lt;span class="k"&gt;select&lt;/span&gt; &lt;span class="n"&gt;signatures&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;hex&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;substr&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;export_signatures&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;16&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
  &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;qsys2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;program_info&lt;/span&gt;
 &lt;span class="k"&gt;where&lt;/span&gt; &lt;span class="n"&gt;program_library&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'MYLIB'&lt;/span&gt; &lt;span class="k"&gt;and&lt;/span&gt; &lt;span class="n"&gt;program_name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'SIGSRV'&lt;/span&gt;
   &lt;span class="k"&gt;and&lt;/span&gt; &lt;span class="n"&gt;object_type&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'*SRVPGM'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="c1"&gt;-- 1   00000000000000000000000CDE436702&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With &lt;code&gt;EXPORT(*ALL)&lt;/code&gt; the signature is generated from the export list. The two values match, so the call works.&lt;/p&gt;

&lt;h2&gt;
  
  
  Experiment 1: add a procedure, keep EXPORT(*ALL)
&lt;/h2&gt;

&lt;p&gt;A new requirement arrives, so &lt;code&gt;SigC&lt;/code&gt; is added at the end of the source and the service program is rebuilt the same way, &lt;code&gt;EXPORT(*ALL)&lt;/code&gt;. &lt;code&gt;SIGCALL&lt;/code&gt; is not touched. It does not even use &lt;code&gt;SigC&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SIGSRV signature now   0000000000000000000000CDA7FD3902
SIGCALL still expects  00000000000000000000000CDE436702

CALL SIGCALL  -&amp;gt;  MCH4431 Program signature violation.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is the most common way to meet &lt;code&gt;MCH4431&lt;/code&gt;. &lt;strong&gt;With &lt;code&gt;EXPORT(*ALL)&lt;/code&gt;, every new procedure changes the signature, so every program bound to the service program has to be rebound,&lt;/strong&gt; including the ones that will never call the new procedure.&lt;/p&gt;

&lt;h2&gt;
  
  
  Experiment 2: binder source with a *PRV block
&lt;/h2&gt;

&lt;p&gt;The fix is to stop letting the system guess and write the export list yourself in binder source:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;STRPGMEXP  PGMLVL(*CURRENT) SIGNATURE('SIGSRV V2')
  EXPORT     SYMBOL(SIGA)
  EXPORT     SYMBOL(SIGB)
  EXPORT     SYMBOL(SIGC)
ENDPGMEXP
STRPGMEXP  PGMLVL(*PRV)
  EXPORT     SYMBOL(SIGA)
  EXPORT     SYMBOL(SIGB)
ENDPGMEXP
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;CRTSRVPGM SRVPGM(MYLIB/SIGSRV) MODULE(QTEMP/SIGSRV) +
          EXPORT(*SRCFILE) SRCFILE(MYLIB/QSRVSRC) SRCMBR(SIGSRV)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;*PRV&lt;/code&gt; block has no &lt;code&gt;SIGNATURE&lt;/code&gt; keyword, so the system generates one from its list, the same way &lt;code&gt;EXPORT(*ALL)&lt;/code&gt; did for the old two-procedure version. The result:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SIGSRV signatures      2
  current              E2C9C7E2D9E540E5F240404040404040   ('SIGSRV V2' in EBCDIC)
  previous             00000000000000000000000CDE436702
SIGCALL still expects  00000000000000000000000CDE436702

CALL SIGCALL  -&amp;gt;  SigA=A SigB=B
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The generated signature of the old list is &lt;strong&gt;exactly&lt;/strong&gt; the one the existing caller stored. &lt;code&gt;SIGCALL&lt;/code&gt; runs again, without being recompiled or rebound. This is a useful rescue when a service program was built with &lt;code&gt;EXPORT(*ALL)&lt;/code&gt; for years: describe the old export list in a &lt;code&gt;*PRV&lt;/code&gt; block, in its original order, and every existing caller keeps working.&lt;/p&gt;

&lt;h2&gt;
  
  
  Experiment 3: a fixed signature, new procedure at the end
&lt;/h2&gt;

&lt;p&gt;Now &lt;code&gt;SIGCALL&lt;/code&gt; is rebound once, so it stores &lt;code&gt;'SIGSRV V2'&lt;/code&gt;. Then &lt;code&gt;SigD&lt;/code&gt; is added, and the binder source only gets one new line, at the end. The signature stays the same:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;STRPGMEXP  PGMLVL(*CURRENT) SIGNATURE('SIGSRV V2')
  EXPORT     SYMBOL(SIGA)
  EXPORT     SYMBOL(SIGB)
  EXPORT     SYMBOL(SIGC)
  EXPORT     SYMBOL(SIGD)
ENDPGMEXP
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;CALL SIGCALL  -&amp;gt;  SigA=A SigB=B
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;No rebind, no &lt;code&gt;*PRV&lt;/code&gt; block, no error. Positions 1 and 2 are still &lt;code&gt;SigA&lt;/code&gt; and &lt;code&gt;SigB&lt;/code&gt;, and the caller only knows positions. This is the pattern most shops settle on: &lt;strong&gt;a hard-coded signature, and new exports only ever added at the end.&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Experiment 4: reorder the exports, keep the signature
&lt;/h2&gt;

&lt;p&gt;This is the one that worries me. Someone tidies up the binder source and puts the exports in a different order. The signature literal stays &lt;code&gt;'SIGSRV V2'&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;STRPGMEXP  PGMLVL(*CURRENT) SIGNATURE('SIGSRV V2')
  EXPORT     SYMBOL(SIGB)
  EXPORT     SYMBOL(SIGA)
  EXPORT     SYMBOL(SIGC)
  EXPORT     SYMBOL(SIGD)
ENDPGMEXP
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;CALL SIGCALL  -&amp;gt;  SigA=B SigB=A
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;No &lt;code&gt;MCH4431&lt;/code&gt;, no message in the job log, no error at all. The signature still matches, so the system trusts the caller. The caller asks for "export number 1", which is now &lt;code&gt;SigB&lt;/code&gt;. &lt;strong&gt;The program calls the wrong procedure and carries on.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In this demo both procedures have the same interface, so it only returns a wrong letter. In a real service program the swapped procedures usually have different parameters. Then you get a wrong amount, a decimal data error far away from the cause, or a pointer error that nobody can explain, and only in the programs that were bound before the change.&lt;/p&gt;

&lt;p&gt;A fixed signature is a promise: "the first N exports are still the same procedures in the same order." The system cannot check that promise for you. That is the trade-off you accept when you stop using &lt;code&gt;*GEN&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The same thing happens if a procedure is &lt;strong&gt;removed&lt;/strong&gt; from the middle of the list: every procedure after it moves up one position.&lt;/p&gt;

&lt;h2&gt;
  
  
  Finding the programs that will fail before they fail
&lt;/h2&gt;

&lt;p&gt;Because both signatures are visible through SQL, you can check every caller before a release instead of waiting for &lt;code&gt;MCH4431&lt;/code&gt; in production:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;select&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;program_library&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;program_name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
       &lt;span class="n"&gt;hex&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;bound_service_program_signature&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;caller_signature&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
       &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="k"&gt;when&lt;/span&gt; &lt;span class="n"&gt;locate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;cast&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;bound_service_program_signature&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nb"&gt;blob&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;16&lt;/span&gt;&lt;span class="p"&gt;)),&lt;/span&gt;
                        &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;export_signatures&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
            &lt;span class="k"&gt;then&lt;/span&gt; &lt;span class="s1"&gt;'OK'&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="s1"&gt;'WILL FAIL MCH4431'&lt;/span&gt; &lt;span class="k"&gt;end&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;status&lt;/span&gt;
  &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;qsys2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;bound_srvpgm_info&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;
  &lt;span class="k"&gt;join&lt;/span&gt; &lt;span class="n"&gt;qsys2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;program_info&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;
    &lt;span class="k"&gt;on&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;program_library&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;bound_service_program_library&lt;/span&gt;
   &lt;span class="k"&gt;and&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;program_name&lt;/span&gt;    &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;bound_service_program&lt;/span&gt;
   &lt;span class="k"&gt;and&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;object_type&lt;/span&gt;     &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'*SRVPGM'&lt;/span&gt;
 &lt;span class="k"&gt;where&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;bound_service_program_library&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'MYLIB'&lt;/span&gt;
   &lt;span class="k"&gt;and&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;bound_service_program&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'SIGSRV'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After experiment 1 it returned &lt;code&gt;SIGCALL ... WILL FAIL MCH4431&lt;/code&gt;. After experiment 3 it returned &lt;code&gt;OK&lt;/code&gt;. Two notes: &lt;code&gt;BOUND_SRVPGM_INFO&lt;/code&gt; reads program objects, so on a large system filter by &lt;code&gt;program_library&lt;/code&gt; as well. And it says &lt;code&gt;OK&lt;/code&gt; after experiment 4 too, because the signature matches. The query finds the loud failures, not the silent one.&lt;/p&gt;

&lt;h2&gt;
  
  
  The rules I follow
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Use binder source, not &lt;code&gt;EXPORT(*ALL)&lt;/code&gt;,&lt;/strong&gt; for any service program that has more than one caller.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Add new exports at the end only.&lt;/strong&gt; Never insert in the middle, never reorder, never delete. A procedure you no longer want stays in the list and can return an error.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Choose one signature style per service program.&lt;/strong&gt; Either a hard-coded signature that never changes (simple, but the order is your responsibility), or a new &lt;code&gt;*CURRENT&lt;/code&gt; signature per release with every older list kept as &lt;code&gt;*PRV&lt;/code&gt; (the system checks more for you, and the binder source grows over time).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;If you inherit a service program built with &lt;code&gt;EXPORT(*ALL)&lt;/code&gt;,&lt;/strong&gt; describe its current export list in a &lt;code&gt;*PRV&lt;/code&gt; block in the same order before you add anything. The existing callers keep their generated signature.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Before a release, run the query above&lt;/strong&gt; for every service program that changed.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;When you see &lt;code&gt;MCH4431&lt;/code&gt;, do not only rebind the program that failed.&lt;/strong&gt; Find out why the signature changed, because every other caller has the same problem.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;All results in this article come from a scripted run on PUB400: create the service program, bind the caller, change one thing, call, and read the data area and &lt;code&gt;QSYS2&lt;/code&gt; views after each step. If your shop handles signatures differently, for example one signature per release with a growing list of &lt;code&gt;*PRV&lt;/code&gt; blocks, I would like to hear how that works for you after a few years.&lt;/p&gt;

</description>
      <category>programming</category>
      <category>rpg</category>
      <category>as400</category>
      <category>ibmi</category>
    </item>
    <item>
      <title>Modernizing a legacy RPG application in five safe steps, with a regression run after each one</title>
      <dc:creator>Dodik Setyawan</dc:creator>
      <pubDate>Sat, 07 Nov 2026 17:44:59 +0000</pubDate>
      <link>https://dev.to/dodikibmi/modernizing-a-legacy-rpg-application-in-five-safe-steps-with-a-regression-run-after-each-one-3j0b</link>
      <guid>https://dev.to/dodikibmi/modernizing-a-legacy-rpg-application-in-five-safe-steps-with-a-regression-run-after-each-one-3j0b</guid>
      <description>&lt;p&gt;"Modernize the RPG" is easy to say and risky to do. The programs that need it most are the ones that have run the business for twenty years, with rules nobody has written down anywhere except in the calculation specs.&lt;/p&gt;

&lt;p&gt;The way I approach it is boring on purpose: &lt;strong&gt;many small steps, and after every step a regression run that must give exactly the same result as the legacy code.&lt;/strong&gt; If the output differs, the step is not finished.&lt;/p&gt;

&lt;p&gt;To show the method end to end, I built a small "legacy" accounts receivable application the way many shops still run it (fixed-format RPG, numbered indicators, the RPG cycle, O-specs, DDS files) and modernized it in five steps on &lt;a href="https://pub400.com" rel="noopener noreferrer"&gt;PUB400&lt;/a&gt;, IBM i 7.5. Everything is on GitHub: &lt;a href="https://github.com/dodikibmi/ibmi-modernization-showcase" rel="noopener noreferrer"&gt;dodikibmi/ibmi-modernization-showcase&lt;/a&gt;. All names and data are fictional.&lt;/p&gt;

&lt;h2&gt;
  
  
  The application and the safety net
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Object&lt;/th&gt;
&lt;th&gt;What it does&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;ARR200&lt;/code&gt; (RPGLE)&lt;/td&gt;
&lt;td&gt;posts each new payment to the customer's oldest open invoices; rejects amounts ≤ 0 and unknown customers; leftover money becomes unapplied credit; releases a customer from hold when the balance reaches zero&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;ARR300&lt;/code&gt; (RPGLE)&lt;/td&gt;
&lt;td&gt;aged trial balance driven by the RPG cycle: primary file, L1 control break, O-specs&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;ARC100&lt;/code&gt; (CL)&lt;/td&gt;
&lt;td&gt;the daily run: post payments, then print the report&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;ARCUSTP&lt;/code&gt;, &lt;code&gt;ARINVP&lt;/code&gt;, &lt;code&gt;ARINVL1&lt;/code&gt;, &lt;code&gt;ARPAYP&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;DDS physical and logical files&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The safety net is a small harness: load the same test data (customers, invoices and seven payments including the nasty ones: zero amount, unknown customer, overpayment), run the daily job, then save &lt;strong&gt;every row of the three files plus the spooled report&lt;/strong&gt; to a text file. The legacy run is the baseline; each later stage must produce an identical file.&lt;/p&gt;

&lt;p&gt;That one rule shaped every decision below.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 1: fixed format to fully free RPG, behaviour unchanged
&lt;/h2&gt;

&lt;p&gt;Before:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;     C     PYCUST        CHAIN     ARCUSTR                            90
     C     *IN90         IFEQ      *ON
     C                   MOVE      'E'           PYSTS
     C                   MOVEL     MSGCUS        PYMSG
     C                   UPDATE    ARPAYR
     C                   GOTO      ENDPST
     C                   ENDIF
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;chain PYCUST ARCUSTR;
if not %found(ARCUSTP);
  rejectPayment(MSG_NO_CUSTOMER);
  return;
endif;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Six numbered indicators became &lt;code&gt;%found&lt;/code&gt;, &lt;code&gt;%eof&lt;/code&gt; and named variables, two &lt;code&gt;GOTO&lt;/code&gt;/&lt;code&gt;TAG&lt;/code&gt; became an early &lt;code&gt;return&lt;/code&gt; from a procedure, and one subroutine became six procedures with parameters and local variables.&lt;/p&gt;

&lt;p&gt;The payment program converted almost line by line. The report did not: fully free RPG has no I- or O-specs, so a cycle program has to be &lt;strong&gt;restructured&lt;/strong&gt; into a read loop with a customer break and an externally described printer file. To prove that the new printer file prints exactly what the old O-specs printed, I generated both layouts from one table and compared the spooled output line by line.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The defect I kept on purpose.&lt;/strong&gt; The legacy report prints an invoice of a customer that is missing from the master file as &lt;code&gt;100500 *UNKNOWN* (FICTIONAL)&lt;/code&gt;. A failed &lt;code&gt;CHAIN&lt;/code&gt; leaves the previous customer's name in the buffer, and &lt;code&gt;MOVEL '*UNKNOWN*'&lt;/code&gt; only overwrites the first nine characters. Step 1 is a pure refactor, so I kept the bug (and marked it in the code). If you change structure and behaviour in the same step, a difference in the regression run no longer tells you which of the two caused it.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 2: DDS to SQL tables, without recompiling a single program
&lt;/h2&gt;

&lt;p&gt;The new tables have long names, long column names (&lt;code&gt;CUSTOMER_ID FOR COLUMN CUCUST&lt;/code&gt;), primary keys, &lt;code&gt;CHECK&lt;/code&gt; constraints, and audit columns maintained by Db2 itself (&lt;code&gt;ROW CHANGE TIMESTAMP&lt;/code&gt;, &lt;code&gt;GENERATED ALWAYS AS (USER)&lt;/code&gt;), declared &lt;code&gt;IMPLICITLY HIDDEN&lt;/code&gt; so the old programs never see them.&lt;/p&gt;

&lt;p&gt;The hard requirement: the legacy programs and the step 1 programs must keep running &lt;strong&gt;without recompiling&lt;/strong&gt;. A program compiled against a file checks the record format &lt;strong&gt;level identifier&lt;/strong&gt; when it opens it; if it changed, you get &lt;code&gt;CPF4131&lt;/code&gt; until every program is recompiled. So each old file name comes back as an object over the new table, with the old record format, and the level identifier must be the same as before.&lt;/p&gt;

&lt;p&gt;My first attempt used SQL indexes for all four files (&lt;code&gt;CREATE INDEX ... RCDFMT ... ADD &amp;lt;columns&amp;gt;&lt;/code&gt;). Two of them got &lt;strong&gt;new&lt;/strong&gt; level identifiers. The reason: an SQL index always places its key columns first in its record format, and in the invoice file the key is not the first field. For those two I used DDS logical files over the SQL table, which keep the original field order. The result (saved in &lt;code&gt;test/out/level-ids.txt&lt;/code&gt;): four old level identifiers, four identical new ones, and both the legacy and the free programs ran against the new tables with output identical to the baseline.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 3: business rules in a service program, with unit tests, and the bug fixed
&lt;/h2&gt;

&lt;p&gt;The rules that were buried in calculation specs moved into a NOMAIN service program with binder source and an explicit signature: validate a payment, how much of a payment goes to one invoice, when a customer leaves hold, days past due, the age bucket, the name to print.&lt;/p&gt;

&lt;p&gt;None of these procedures does file I/O, so they can be tested without any test data. An &lt;a href="https://github.com/tools-400/irpgunit" rel="noopener noreferrer"&gt;iRPGUnit&lt;/a&gt; suite covers every rule, every bucket boundary (0/1, 30/31, 60/61, 90/91), a leap day, and the old &lt;code&gt;*UNKNOWN*&lt;/code&gt; defect:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Success. 20 test cases, 28 assertions, 0 failure, 0 error.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Now the bug is fixed, in its own step.&lt;/strong&gt; The regression run differs from the legacy output in exactly one line, the one that was wrong. Every payment, invoice and balance is still identical. That single-line diff is the best evidence I know that a fix did what it should and nothing else.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 4: native record I/O to embedded SQL, one transaction per payment
&lt;/h2&gt;

&lt;p&gt;The legacy program had no commitment control. If it failed after updating an invoice but before updating the customer, the data was out of balance and someone fixed it by hand.&lt;/p&gt;

&lt;p&gt;The SQL version works on the new tables by their long names and still takes every rule from the service program:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;a cursor over waiting payments, declared &lt;code&gt;WITH HOLD&lt;/code&gt; so it survives each &lt;code&gt;COMMIT&lt;/code&gt;;&lt;/li&gt;
&lt;li&gt;the customer row read &lt;code&gt;WITH RS USE AND KEEP EXCLUSIVE LOCKS&lt;/code&gt;, then updated;&lt;/li&gt;
&lt;li&gt;open invoices from a cursor &lt;code&gt;ORDER BY DUE_DATE FOR UPDATE&lt;/code&gt;, updated &lt;code&gt;WHERE CURRENT OF&lt;/code&gt;;&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;COMMIT&lt;/code&gt; after each payment; any SQL error means &lt;code&gt;ROLLBACK&lt;/code&gt; and an escape message.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A regression run proves the result is the same. But a transaction is only proven when it rolls back, so I tested that too: another job held a lock on the second invoice of a customer. The run paid the first invoice in full, waited for the second, timed out and ended with an escape message. Afterwards the first invoice was still unpaid, the balance unchanged and the payment still waiting: the half-posted payment was rolled back. Once the lock was gone, the same command posted everything and the output matched the normal run.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 5: a REST API that reuses the RPG code
&lt;/h2&gt;

&lt;p&gt;The last step adds a REST API in Python on PASE, using the standard library HTTP server and &lt;a href="https://github.com/IBM/python-itoolkit" rel="noopener noreferrer"&gt;itoolkit&lt;/a&gt;. The point is that &lt;strong&gt;no business rule is written twice&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;the API validates a payment by calling the service program procedure directly from Python, so the API and the batch can never disagree;&lt;/li&gt;
&lt;li&gt;an accepted payment is inserted and then posted by &lt;strong&gt;the batch program itself&lt;/strong&gt;, with its one-transaction-per-payment logic;&lt;/li&gt;
&lt;li&gt;age buckets come from the same procedure the report uses, all calls of one request batched into a single round trip.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The test sends the seven test payments one at a time through &lt;code&gt;POST /api/payments&lt;/code&gt;. The status codes come back as expected (&lt;code&gt;201 201 201 404 422 201 422&lt;/code&gt;), and the customers and invoices are identical to the step 4 batch run.&lt;/p&gt;

&lt;h2&gt;
  
  
  Before and after
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;Legacy&lt;/th&gt;
&lt;th&gt;Modernized&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Numbered indicators&lt;/td&gt;
&lt;td&gt;6 + &lt;code&gt;L1&lt;/code&gt;, &lt;code&gt;LR&lt;/code&gt;, &lt;code&gt;1P&lt;/code&gt;, &lt;code&gt;OF&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;GOTO&lt;/code&gt; / &lt;code&gt;TAG&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Business rules&lt;/td&gt;
&lt;td&gt;inside the calc specs of two programs&lt;/td&gt;
&lt;td&gt;6 procedures, used by the batch and the API&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Unit tests&lt;/td&gt;
&lt;td&gt;none&lt;/td&gt;
&lt;td&gt;20 test cases, 28 assertions&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Database&lt;/td&gt;
&lt;td&gt;DDS, no constraints&lt;/td&gt;
&lt;td&gt;SQL tables, keys, &lt;code&gt;CHECK&lt;/code&gt; constraints, audit columns, journaled&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Programs recompiled for the DDS → SQL move&lt;/td&gt;
&lt;td&gt;—&lt;/td&gt;
&lt;td&gt;none&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Failure halfway through a payment&lt;/td&gt;
&lt;td&gt;data out of balance&lt;/td&gt;
&lt;td&gt;rolled back&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Known defect&lt;/td&gt;
&lt;td&gt;prints the previous customer's name&lt;/td&gt;
&lt;td&gt;fixed in its own step, covered by a test&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h2&gt;
  
  
  What I would tell a team starting this
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Build the regression harness first.&lt;/strong&gt; Same test data, same run, every row and the spooled report saved as text. It costs a day and pays for itself in the first step.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Never change structure and behaviour in the same step.&lt;/strong&gt; Keep the known bugs during the refactor, then fix each one in its own step with a test. A one-line diff is a proof; a fifty-line diff is a discussion.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Watch the level identifiers when you move DDS to SQL.&lt;/strong&gt; An SQL index puts its keys first; where the keys are not the leading fields, a DDS logical file over the SQL table keeps the old format and saves you a recompile of every program.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Prove the rollback, not only the commit.&lt;/strong&gt; Hold a lock from another job and watch the transaction undo itself.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;New channels call the existing rules.&lt;/strong&gt; The API is valuable because it reuses the RPG procedures and the batch program, not because it is written in Python.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The code, the test data and the saved output of every regression run are in the repository: &lt;a href="https://github.com/dodikibmi/ibmi-modernization-showcase" rel="noopener noreferrer"&gt;github.com/dodikibmi/ibmi-modernization-showcase&lt;/a&gt;. I would be glad to hear how other shops structure this kind of work, especially the DDS to SQL move on files with many dependent programs.&lt;/p&gt;

</description>
      <category>ibmi</category>
      <category>rpg</category>
      <category>db2</category>
      <category>modernization</category>
    </item>
    <item>
      <title>Calling REST APIs from RPG without HTTPAPI: QSYS2.HTTP_GET, JSON_TABLE and HTTP_POST</title>
      <dc:creator>Dodik Setyawan</dc:creator>
      <pubDate>Sun, 01 Nov 2026 15:55:01 +0000</pubDate>
      <link>https://dev.to/dodikibmi/calling-rest-apis-from-rpg-without-httpapi-qsys2httpget-jsontable-and-httppost-2kc2</link>
      <guid>https://dev.to/dodikibmi/calling-rest-apis-from-rpg-without-httpapi-qsys2httpget-jsontable-and-httppost-2kc2</guid>
      <description>&lt;p&gt;For years, an RPG program that had to call a web service needed extra pieces: Scott Klement's HTTPAPI, raw sockets, or the Java-based &lt;code&gt;SYSTOOLS.HTTPGETCLOB&lt;/code&gt;. They all work, but each one is something more to install, learn and support.&lt;/p&gt;

&lt;p&gt;On current IBM i releases (7.3 TR11 / 7.4 TR5 and later) Db2 for i can do it alone. The &lt;code&gt;QSYS2.HTTP_GET&lt;/code&gt;, &lt;code&gt;HTTP_POST&lt;/code&gt;, &lt;code&gt;HTTP_PUT&lt;/code&gt;, &lt;code&gt;HTTP_DELETE&lt;/code&gt; functions and their &lt;code&gt;_VERBOSE&lt;/code&gt; table versions call a URL from SQL, and the JSON functions turn the answer into rows. From RPG that is just embedded SQL.&lt;/p&gt;

&lt;p&gt;In this post I show both directions from my sample project &lt;a href="https://github.com/dodikibmi/ibmi-inventory" rel="noopener noreferrer"&gt;dodikibmi/ibmi-inventory&lt;/a&gt;, running on &lt;a href="https://pub400.com" rel="noopener noreferrer"&gt;PUB400&lt;/a&gt; (IBM i 7.5):&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;GET&lt;/strong&gt;: load public holidays from a REST API into a Db2 table (&lt;code&gt;INV600R&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;POST&lt;/strong&gt;: send a "reorder needed" alert as JSON to a webhook (&lt;code&gt;INV700R&lt;/code&gt;).&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;And one trap I fell into: &lt;strong&gt;a 404 answer does not raise an SQL error.&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 1: try it in plain SQL first
&lt;/h2&gt;

&lt;p&gt;Before writing any RPG, run the call in &lt;em&gt;Run SQL Scripts&lt;/em&gt; or the Code for IBM i SQL view. The free &lt;a href="https://date.nager.at" rel="noopener noreferrer"&gt;Nager.Date&lt;/a&gt; API returns the public holidays of a country as a JSON array (shortened here):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nl"&gt;"date"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"2026-01-01"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nl"&gt;"localName"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"Tahun Baru Masehi"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nl"&gt;"name"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"New Year's Day"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nl"&gt;"countryCode"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"ID"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nl"&gt;"types"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"Public"&lt;/span&gt;&lt;span class="p"&gt;]},&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nl"&gt;"date"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"2026-04-03"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nl"&gt;"localName"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"Wafat Isa Almasih"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nl"&gt;"name"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"Good Friday"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nl"&gt;"countryCode"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"ID"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nl"&gt;"types"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"Public"&lt;/span&gt;&lt;span class="p"&gt;]}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;JSON_TABLE&lt;/code&gt; maps that array to columns, and &lt;code&gt;HTTP_GET&lt;/code&gt; can be its input directly:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;h&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;hdate&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;h&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;hname&lt;/span&gt;
  &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;JSON_TABLE&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
         &lt;span class="n"&gt;QSYS2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;HTTP_GET&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'https://date.nager.at/api/v3/PublicHolidays/2026/ID'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;''&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
         &lt;span class="s1"&gt;'lax $[*]'&lt;/span&gt;
         &lt;span class="n"&gt;COLUMNS&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;hdate&lt;/span&gt; &lt;span class="nb"&gt;DATE&lt;/span&gt;         &lt;span class="n"&gt;PATH&lt;/span&gt; &lt;span class="s1"&gt;'lax $.date'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                  &lt;span class="n"&gt;hname&lt;/span&gt; &lt;span class="nb"&gt;VARCHAR&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;PATH&lt;/span&gt; &lt;span class="s1"&gt;'lax $.name'&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="n"&gt;h&lt;/span&gt;
 &lt;span class="k"&gt;ORDER&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="n"&gt;h&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;hdate&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Result on PUB400:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;HDATE       HNAME
2026-01-01  New Year's Day
2026-04-03  Good Friday
2026-04-05  Easter Sunday
2026-05-01  Labour Day
2026-05-14  Ascension Day
2026-06-01  Pancasila Day
2026-08-17  Independence Day
2026-12-25  Christmas Day
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A few notes on the SQL:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;'lax $[*]'&lt;/code&gt; means "every element of the top-level array". &lt;code&gt;lax&lt;/code&gt; mode returns NULL for a missing field instead of failing the whole statement.&lt;/li&gt;
&lt;li&gt;The column type does the conversion: &lt;code&gt;DATE&lt;/code&gt; accepts the ISO string &lt;code&gt;2026-01-01&lt;/code&gt; as it is.&lt;/li&gt;
&lt;li&gt;The second argument of &lt;code&gt;HTTP_GET&lt;/code&gt; is the options JSON. Empty is fine for a public GET.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Once the query returns the right rows, the RPG part is small.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 2: the same call inside RPG, with MERGE
&lt;/h2&gt;

&lt;p&gt;The program loads this year and next year into &lt;code&gt;HOLIDAY_CALENDAR&lt;/code&gt;. &lt;code&gt;MERGE&lt;/code&gt; inserts new dates and updates the names of existing ones, so the program can run every month without creating duplicates:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;exec&lt;/span&gt; &lt;span class="k"&gt;sql&lt;/span&gt;
   &lt;span class="n"&gt;merge&lt;/span&gt; &lt;span class="k"&gt;into&lt;/span&gt; &lt;span class="n"&gt;HOLIDAY_CALENDAR&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt;
   &lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;select&lt;/span&gt; &lt;span class="n"&gt;h&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;hdate&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;min&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;substr&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;h&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;hname&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;30&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;hname&lt;/span&gt;
            &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;json_table&lt;/span&gt;&lt;span class="p"&gt;(:&lt;/span&gt;&lt;span class="n"&gt;Json&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'lax $[*]'&lt;/span&gt;
                   &lt;span class="n"&gt;columns&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;hdate&lt;/span&gt; &lt;span class="nb"&gt;date&lt;/span&gt;         &lt;span class="n"&gt;path&lt;/span&gt; &lt;span class="s1"&gt;'lax $.date'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                            &lt;span class="n"&gt;hname&lt;/span&gt; &lt;span class="nb"&gt;varchar&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;path&lt;/span&gt; &lt;span class="s1"&gt;'lax $.name'&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="n"&gt;h&lt;/span&gt;
           &lt;span class="k"&gt;where&lt;/span&gt; &lt;span class="n"&gt;h&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;hdate&lt;/span&gt; &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="k"&gt;not&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt;
           &lt;span class="k"&gt;group&lt;/span&gt; &lt;span class="k"&gt;by&lt;/span&gt; &lt;span class="n"&gt;h&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;hdate&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;
      &lt;span class="k"&gt;on&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;HLDATE&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;hdate&lt;/span&gt;
    &lt;span class="k"&gt;when&lt;/span&gt; &lt;span class="n"&gt;matched&lt;/span&gt; &lt;span class="k"&gt;then&lt;/span&gt;
         &lt;span class="k"&gt;update&lt;/span&gt; &lt;span class="k"&gt;set&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;HLDESC&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;hname&lt;/span&gt;
    &lt;span class="k"&gt;when&lt;/span&gt; &lt;span class="k"&gt;not&lt;/span&gt; &lt;span class="n"&gt;matched&lt;/span&gt; &lt;span class="k"&gt;then&lt;/span&gt;
         &lt;span class="k"&gt;insert&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;HLDATE&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;HLDESC&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;values&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;hdate&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;hname&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Two details matter here:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;GROUP BY h.hdate&lt;/code&gt;&lt;/strong&gt;: some countries have two holidays on the same day. Without the grouping, &lt;code&gt;MERGE&lt;/code&gt; sees the same key twice and fails. &lt;code&gt;MIN()&lt;/code&gt; simply picks one name.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;substr(..., 1, 30)&lt;/code&gt;&lt;/strong&gt;: the table column is &lt;code&gt;CHAR(30)&lt;/code&gt;. Cut the text in SQL rather than getting a truncation error.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;After the statement, &lt;code&gt;GET DIAGNOSTICS :WkRows = ROW_COUNT&lt;/code&gt; gives the number of rows merged, which the program returns to its caller.&lt;/p&gt;

&lt;p&gt;Once the table is filled, the date service program of the application (&lt;code&gt;DateIsWorkday&lt;/code&gt;, &lt;code&gt;DateAddWdays&lt;/code&gt;) knows the new holidays at once, without a recompile. That is the real value: the external data lands in an ordinary Db2 table that any RPG program can &lt;code&gt;CHAIN&lt;/code&gt; to.&lt;/p&gt;

&lt;h2&gt;
  
  
  The trap: a 404 is not an SQL error
&lt;/h2&gt;

&lt;p&gt;My first version passed &lt;code&gt;QSYS2.HTTP_GET(:WkUrl, '')&lt;/code&gt; directly into &lt;code&gt;JSON_TABLE&lt;/code&gt;, like the SQL above. It worked fine until I tested a wrong URL (country code &lt;code&gt;XX&lt;/code&gt;):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;VALUES&lt;/span&gt; &lt;span class="n"&gt;QSYS2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;HTTP_GET&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'https://date.nager.at/api/v3/PublicHolidays/2026/XX'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;''&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{"title":"Unknown country code","status":404,"detail":"The country code provided is invalid or not recognized."}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;No SQLCODE, no exception: &lt;code&gt;HTTP_GET&lt;/code&gt; simply returned the body of the error answer. &lt;code&gt;JSON_TABLE&lt;/code&gt; read that object, found no &lt;code&gt;date&lt;/code&gt;, the &lt;code&gt;WHERE h.hdate IS NOT NULL&lt;/code&gt; filtered it out, and &lt;code&gt;MERGE&lt;/code&gt; reported &lt;strong&gt;0 rows&lt;/strong&gt;. A batch job would end "normally" every night and load nothing.&lt;/p&gt;

&lt;p&gt;The fix is the &lt;code&gt;_VERBOSE&lt;/code&gt; version. It is a table function that returns the body &lt;strong&gt;and&lt;/strong&gt; the response headers as JSON, and the headers include &lt;code&gt;HTTP_STATUS_CODE&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;RESPONSE_HTTP_HEADER&lt;/span&gt;
  &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;QSYS2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;HTTP_GET_VERBOSE&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
         &lt;span class="s1"&gt;'https://date.nager.at/api/v3/PublicHolidays/2026/XX'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;''&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{"HTTP_STATUS_CODE":404,"Content-Type":"application/json; charset=utf-8", ...}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So the RPG program now calls the API once, checks the status, and only then parses the body:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="o"&gt;//&lt;/span&gt; &lt;span class="k"&gt;Call&lt;/span&gt; &lt;span class="n"&gt;the&lt;/span&gt; &lt;span class="n"&gt;API&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;the&lt;/span&gt; &lt;span class="n"&gt;status&lt;/span&gt; &lt;span class="n"&gt;code&lt;/span&gt; &lt;span class="n"&gt;comes&lt;/span&gt; &lt;span class="n"&gt;back&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;the&lt;/span&gt; &lt;span class="n"&gt;header&lt;/span&gt; &lt;span class="n"&gt;JSON&lt;/span&gt;
&lt;span class="k"&gt;exec&lt;/span&gt; &lt;span class="k"&gt;sql&lt;/span&gt;
   &lt;span class="k"&gt;select&lt;/span&gt; &lt;span class="n"&gt;json_value&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;RESPONSE_HTTP_HEADER&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                     &lt;span class="s1"&gt;'$.HTTP_STATUS_CODE'&lt;/span&gt; &lt;span class="n"&gt;returning&lt;/span&gt; &lt;span class="nb"&gt;integer&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
          &lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;RESPONSE_MESSAGE&lt;/span&gt;
     &lt;span class="k"&gt;into&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="n"&gt;WkStatus&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="n"&gt;Json&lt;/span&gt;
     &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="k"&gt;table&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;QSYS2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;HTTP_GET_VERBOSE&lt;/span&gt;&lt;span class="p"&gt;(:&lt;/span&gt;&lt;span class="n"&gt;WkUrl&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;''&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;if&lt;/span&gt; &lt;span class="k"&gt;SQLCODE&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
   &lt;span class="n"&gt;WkMsg&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'Calling '&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;WkUrl&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s1"&gt;' failed, SQLSTATE '&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="k"&gt;SQLSTATE&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
   &lt;span class="n"&gt;exsr&lt;/span&gt; &lt;span class="n"&gt;SendEscape&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;endif&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;if&lt;/span&gt; &lt;span class="n"&gt;WkStatus&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;200&lt;/span&gt; &lt;span class="k"&gt;or&lt;/span&gt; &lt;span class="n"&gt;WkStatus&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;299&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
   &lt;span class="n"&gt;WkMsg&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'Holiday API answered HTTP '&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt;&lt;span class="nb"&gt;char&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;WkStatus&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
         &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s1"&gt;' for '&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt;&lt;span class="nb"&gt;char&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;WkYear&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
   &lt;span class="n"&gt;exsr&lt;/span&gt; &lt;span class="n"&gt;SendEscape&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;endif&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;Json&lt;/code&gt; is a CLOB host variable (&lt;code&gt;D Json S SQLTYPE(CLOB:1000000)&lt;/code&gt;), and the &lt;code&gt;MERGE&lt;/code&gt; above reads from &lt;code&gt;:Json&lt;/code&gt; instead of calling the API a second time.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;SendEscape&lt;/code&gt; sends &lt;code&gt;CPF9898&lt;/code&gt; as an escape message with &lt;code&gt;QMHSNDPM&lt;/code&gt;. That is the IBM i way to fail: the CL program that runs the job can &lt;code&gt;MONMSG CPF9898&lt;/code&gt; and alert someone, and the job log shows the real reason.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Rule of thumb: use the &lt;code&gt;_VERBOSE&lt;/code&gt; functions in programs, and keep the short ones for quick queries.&lt;/strong&gt; A network error (DNS, timeout, TLS) does raise an SQL error; an HTTP error status does not.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 3: the other direction, POST with HTTP_POST
&lt;/h2&gt;

&lt;p&gt;The second program pushes data out. Items below their minimum stock are summarised per warehouse and posted as JSON to a webhook, the kind of endpoint Slack, Teams or another application provides.&lt;/p&gt;

&lt;p&gt;Db2 builds the whole message in one statement. &lt;code&gt;LISTAGG&lt;/code&gt; turns several rows into one readable line per warehouse, and &lt;code&gt;JSON_OBJECT&lt;/code&gt; / &lt;code&gt;JSON_ARRAYAGG&lt;/code&gt; wrap it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;exec&lt;/span&gt; &lt;span class="k"&gt;sql&lt;/span&gt;
   &lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="n"&gt;w&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="k"&gt;select&lt;/span&gt; &lt;span class="k"&gt;trim&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;STWHS&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;code&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;trim&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;WHNAME&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
               &lt;span class="k"&gt;count&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;cnt&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
               &lt;span class="n"&gt;listagg&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;trim&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;STITEM&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="s1"&gt;', '&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                  &lt;span class="n"&gt;within&lt;/span&gt; &lt;span class="k"&gt;group&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;order&lt;/span&gt; &lt;span class="k"&gt;by&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;STITEM&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;items&lt;/span&gt;
          &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;STOCK_STATUS&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;
         &lt;span class="k"&gt;where&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;REORDER&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'Y'&lt;/span&gt;
         &lt;span class="k"&gt;group&lt;/span&gt; &lt;span class="k"&gt;by&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;STWHS&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;WHNAME&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
   &lt;span class="k"&gt;select&lt;/span&gt; &lt;span class="n"&gt;coalesce&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;sum&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;w&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;cnt&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
          &lt;span class="n"&gt;json_object&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
            &lt;span class="s1"&gt;'source'&lt;/span&gt;     &lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="s1"&gt;'IBM i Warehouse Inventory'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="s1"&gt;'text'&lt;/span&gt;       &lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="s1"&gt;'Reorder needed: '&lt;/span&gt; &lt;span class="n"&gt;concat&lt;/span&gt;
                          &lt;span class="n"&gt;listagg&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;w&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;code&lt;/span&gt; &lt;span class="n"&gt;concat&lt;/span&gt; &lt;span class="s1"&gt;' ('&lt;/span&gt;
                                  &lt;span class="n"&gt;concat&lt;/span&gt; &lt;span class="n"&gt;w&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;items&lt;/span&gt; &lt;span class="n"&gt;concat&lt;/span&gt; &lt;span class="s1"&gt;')'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'; '&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                             &lt;span class="n"&gt;within&lt;/span&gt; &lt;span class="k"&gt;group&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;order&lt;/span&gt; &lt;span class="k"&gt;by&lt;/span&gt; &lt;span class="n"&gt;w&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;code&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
            &lt;span class="s1"&gt;'warehouses'&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="n"&gt;json_arrayagg&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
                          &lt;span class="n"&gt;json_object&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'code'&lt;/span&gt;      &lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="n"&gt;w&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;code&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                                      &lt;span class="s1"&gt;'name'&lt;/span&gt;      &lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="n"&gt;w&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                                      &lt;span class="s1"&gt;'itemCount'&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="n"&gt;w&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;cnt&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                                      &lt;span class="s1"&gt;'items'&lt;/span&gt;     &lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="n"&gt;w&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;items&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                          &lt;span class="k"&gt;order&lt;/span&gt; &lt;span class="k"&gt;by&lt;/span&gt; &lt;span class="n"&gt;w&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;code&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
     &lt;span class="k"&gt;into&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="n"&gt;WkItems&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="n"&gt;Body&lt;/span&gt;
     &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;w&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then the POST, again with the &lt;code&gt;_VERBOSE&lt;/code&gt; version so the status is checked:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="n"&gt;WkOpt&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'{"header":"content-type,application/json"}'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;exec&lt;/span&gt; &lt;span class="k"&gt;sql&lt;/span&gt;
   &lt;span class="k"&gt;select&lt;/span&gt; &lt;span class="n"&gt;json_value&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;RESPONSE_HTTP_HEADER&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'$.HTTP_STATUS_CODE'&lt;/span&gt;
                     &lt;span class="n"&gt;returning&lt;/span&gt; &lt;span class="nb"&gt;integer&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
     &lt;span class="k"&gt;into&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="n"&gt;WkStatus&lt;/span&gt;
     &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="k"&gt;table&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;QSYS2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;HTTP_POST_VERBOSE&lt;/span&gt;&lt;span class="p"&gt;(:&lt;/span&gt;&lt;span class="n"&gt;WkUrl&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="n"&gt;Body&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="n"&gt;WkOpt&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The options JSON is where the request gets its headers. &lt;code&gt;"header":"name,value"&lt;/code&gt; sets one header; without &lt;code&gt;content-type: application/json&lt;/code&gt; many APIs reject the body or treat it as form data. The same options string also takes settings such as basic authentication and timeouts; the IBM documentation of the HTTP functions lists them all.&lt;/p&gt;

&lt;p&gt;For testing I post to &lt;code&gt;https://httpbin.org/post&lt;/code&gt;, an echo service that returns what it received. The answer on PUB400 was HTTP 200 with the message intact:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"source"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"IBM i Warehouse Inventory"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"text"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"Reorder needed: CTR (EL-0006, OF-0005); EST (CL-0003, EL-0001, EL-0004, OF-0002, PK-0005); WST (CL-0001, OF-0001, PK-0001, PK-0003)"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"warehouses"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nl"&gt;"code"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"CTR"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nl"&gt;"name"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"Central Warehouse"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nl"&gt;"itemCount"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nl"&gt;"items"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"EL-0006, OF-0005"&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nl"&gt;"code"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"EST"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nl"&gt;"name"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"East Distribution Hub"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nl"&gt;"itemCount"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nl"&gt;"items"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"CL-0003, EL-0001, EL-0004, OF-0002, PK-0005"&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nl"&gt;"code"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"WST"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nl"&gt;"name"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"West Distribution Hub"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nl"&gt;"itemCount"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nl"&gt;"items"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"CL-0001, OF-0001, PK-0001, PK-0003"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The real webhook URL is a program parameter, so the same program serves a test and a production endpoint.&lt;/p&gt;

&lt;h2&gt;
  
  
  Practical notes
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Test the SQL alone first.&lt;/strong&gt; If the statement works in Run SQL Scripts, the RPG part is only host variables and error handling.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;CLOB host variables&lt;/strong&gt; (&lt;code&gt;SQLTYPE(CLOB:n)&lt;/code&gt;) hold the request and response bodies. Size them for the largest answer you expect.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Read the status before the body&lt;/strong&gt;, as shown above. Treat anything outside 200–299 as a failure the operator must see.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;TLS&lt;/strong&gt; uses the certificate store of the system. If a call to a new site fails with a TLS error, the certificate authority of that site has to be trusted on the system first (Digital Certificate Manager). On PUB400 the public APIs I used worked out of the box.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Commitment control&lt;/strong&gt;: an HTTP call cannot be rolled back. When a program posts to an external system and updates Db2, decide which side is the master and what happens when the second step fails.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  When I would still use HTTPAPI
&lt;/h2&gt;

&lt;p&gt;The SQL functions cover most "call an API and store the answer" jobs. For very large downloads streamed to the IFS, fine control over the connection, or older releases without the HTTP functions, HTTPAPI is still a good choice. For everything else, Db2 for i calling the service is fewer moving parts and is easy for an SQL-literate team to support.&lt;/p&gt;

&lt;h2&gt;
  
  
  Takeaways
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;QSYS2.HTTP_GET&lt;/code&gt; + &lt;code&gt;JSON_TABLE&lt;/code&gt; + &lt;code&gt;MERGE&lt;/code&gt; loads a REST API into a Db2 table in one statement.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;An HTTP error status is not an SQL error.&lt;/strong&gt; Use &lt;code&gt;HTTP_GET_VERBOSE&lt;/code&gt; / &lt;code&gt;HTTP_POST_VERBOSE&lt;/code&gt; and check &lt;code&gt;HTTP_STATUS_CODE&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;LISTAGG&lt;/code&gt; and &lt;code&gt;JSON_OBJECT&lt;/code&gt; build the outgoing message in SQL, so the RPG code stays short.&lt;/li&gt;
&lt;li&gt;Fail loudly with an escape message, so the CL job and the operator know.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Both programs (&lt;code&gt;INV600R&lt;/code&gt;, &lt;code&gt;INV700R&lt;/code&gt;) and the rest of the application are on GitHub: &lt;strong&gt;&lt;a href="https://github.com/dodikibmi/ibmi-inventory" rel="noopener noreferrer"&gt;github.com/dodikibmi/ibmi-inventory&lt;/a&gt;&lt;/strong&gt;. My previous post covers the opposite direction, a REST API served from IBM i with Python: &lt;a href="https://dev.to/dodikibmi/a-rest-api-on-ibm-i-with-python-and-ibmdb-no-framework-running-in-pase-264n"&gt;A REST API on IBM i with Python and ibm_db&lt;/a&gt;.&lt;/p&gt;




&lt;p&gt;&lt;em&gt;I am an IBM i developer from Indonesia, working with RPG, SQL and CL on Db2 for i. This project is a sample application I built on PUB400 to practise modern IBM i techniques in public.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>ibmi</category>
      <category>rpg</category>
      <category>db2</category>
      <category>api</category>
    </item>
    <item>
      <title>A REST API on IBM i with Python and ibm_db, no framework, running in PASE</title>
      <dc:creator>Dodik Setyawan</dc:creator>
      <pubDate>Sun, 01 Nov 2026 15:35:46 +0000</pubDate>
      <link>https://dev.to/dodikibmi/a-rest-api-on-ibm-i-with-python-and-ibmdb-no-framework-running-in-pase-264n</link>
      <guid>https://dev.to/dodikibmi/a-rest-api-on-ibm-i-with-python-and-ibmdb-no-framework-running-in-pase-264n</guid>
      <description>&lt;p&gt;Most IBM i shops sooner or later get the same request: &lt;em&gt;"Can the web team read this data as JSON?"&lt;/em&gt; The data is in Db2 for i, the rules live in RPG programs, and the new consumer speaks HTTP.&lt;/p&gt;

&lt;p&gt;You do not need middleware or a new server for that. IBM i ships Python and the &lt;code&gt;ibm_db&lt;/code&gt; driver as open-source packages, and PASE runs them next to the database. In this post I build a small REST API for a warehouse inventory application, with nothing but the Python standard library and &lt;code&gt;ibm_db&lt;/code&gt;, and I show the problems I hit on the way.&lt;/p&gt;

&lt;p&gt;All code is from my sample project on GitHub: &lt;a href="https://github.com/dodikibmi/ibmi-inventory" rel="noopener noreferrer"&gt;dodikibmi/ibmi-inventory&lt;/a&gt; (&lt;code&gt;api/server.py&lt;/code&gt;, about 330 lines). It runs on &lt;a href="https://pub400.com" rel="noopener noreferrer"&gt;PUB400&lt;/a&gt;, the free public IBM i.&lt;/p&gt;

&lt;h2&gt;
  
  
  The design in one picture
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;PC / browser ──SSH tunnel──► PASE: python3.9 server.py ──ibm_db──► Db2 for i
                                                                     ▲
5250 green screens (RPG) ───────────────────────────────────────────┘
          both channels post stock movements through ONE SQL procedure
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Three decisions shape the whole thing:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Standard library only.&lt;/strong&gt; &lt;code&gt;http.server&lt;/code&gt; is enough for a demo and for internal tools. No &lt;code&gt;pip install&lt;/code&gt; on a shared system.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Reads are plain SQL&lt;/strong&gt; against tables and a view.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Writes go through an SQL stored procedure&lt;/strong&gt; (&lt;code&gt;POST_MOVEMENT&lt;/code&gt;) that holds the same posting rules as the RPG entry program. The API never updates a table directly, so the green screens and the API cannot drift apart.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Endpoints
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Method&lt;/th&gt;
&lt;th&gt;Path&lt;/th&gt;
&lt;th&gt;Result&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;GET&lt;/td&gt;
&lt;td&gt;&lt;code&gt;/api/health&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;database and schema in use&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;GET&lt;/td&gt;
&lt;td&gt;&lt;code&gt;/api/items?category=OFFC&amp;amp;status=A&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;item list&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;GET&lt;/td&gt;
&lt;td&gt;&lt;code&gt;/api/items/{code}&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;one item with stock per warehouse&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;GET&lt;/td&gt;
&lt;td&gt;&lt;code&gt;/api/stock?warehouse=CTR&amp;amp;reorder=Y&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;stock status, optionally only items to reorder&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;GET&lt;/td&gt;
&lt;td&gt;&lt;code&gt;/api/movements?item=OF-0001&amp;amp;from=2026-09-01&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;movement history&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;POST&lt;/td&gt;
&lt;td&gt;&lt;code&gt;/api/movements&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;post a movement; &lt;code&gt;201&lt;/code&gt; with the new stock&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h2&gt;
  
  
  Install
&lt;/h2&gt;

&lt;p&gt;In an SSH session on the IBM i (or with the ACS Open Source Package Management tool):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;yum &lt;span class="nb"&gt;install &lt;/span&gt;python39 python39-ibm_db
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That is all. &lt;code&gt;/QOpenSys/pkgs/bin/python3.9&lt;/code&gt; now has &lt;code&gt;import ibm_db&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Connecting to the local database
&lt;/h2&gt;

&lt;p&gt;From PASE the driver talks to the Db2 of the same system, so the "database" is the local relational database name (&lt;code&gt;WRKRDBDIRE&lt;/code&gt;, entry &lt;code&gt;*LOCAL&lt;/code&gt;):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;DATABASE&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;environ&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;INV_DB&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;*LOCAL&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;upper&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="n"&gt;DB_USER&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;environ&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;INV_USER&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;""&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;DB_PASSWORD&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;environ&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;INV_PASSWORD&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;""&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;_conn&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="bp"&gt;None&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;connection&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="k"&gt;global&lt;/span&gt; &lt;span class="n"&gt;_conn&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;_conn&lt;/span&gt; &lt;span class="ow"&gt;is&lt;/span&gt; &lt;span class="bp"&gt;None&lt;/span&gt; &lt;span class="ow"&gt;or&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="n"&gt;ibm_db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;active&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;_conn&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="n"&gt;_conn&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;ibm_db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;connect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;DATABASE&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;DB_USER&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;DB_PASSWORD&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="n"&gt;ibm_db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;autocommit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;_conn&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;ibm_db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;SQL_AUTOCOMMIT_OFF&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="n"&gt;ibm_db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;exec_immediate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;_conn&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;SET SCHEMA &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;SCHEMA&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;_conn&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Gotcha 1: an empty user is not always allowed.&lt;/strong&gt; On many systems an empty user and password connect as the user who started the job. PUB400 refuses that with &lt;code&gt;SQL30082&lt;/code&gt;. The fix is to pass a user and password, but never in code: the start script reads the password with &lt;code&gt;read -s&lt;/code&gt; (no echo, not in the shell history) and puts it only in the process environment.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;export &lt;/span&gt;&lt;span class="nv"&gt;INV_USER&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="k"&gt;${&lt;/span&gt;&lt;span class="nv"&gt;INV_USER&lt;/span&gt;&lt;span class="k"&gt;:-&lt;/span&gt;&lt;span class="si"&gt;$(&lt;/span&gt;&lt;span class="nb"&gt;id&lt;/span&gt; &lt;span class="nt"&gt;-un&lt;/span&gt;&lt;span class="si"&gt;)&lt;/span&gt;&lt;span class="k"&gt;}&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt; &lt;span class="nt"&gt;-z&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$INV_PASSWORD&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; &lt;span class="o"&gt;]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;then
  &lt;/span&gt;&lt;span class="nb"&gt;read&lt;/span&gt; &lt;span class="nt"&gt;-r&lt;/span&gt; &lt;span class="nt"&gt;-s&lt;/span&gt; &lt;span class="nt"&gt;-p&lt;/span&gt; &lt;span class="s2"&gt;"Database password for &lt;/span&gt;&lt;span class="nv"&gt;$INV_USER&lt;/span&gt;&lt;span class="s2"&gt;: "&lt;/span&gt; INV_PASSWORD
  &lt;span class="nb"&gt;echo
  export &lt;/span&gt;INV_PASSWORD
&lt;span class="k"&gt;fi
&lt;/span&gt;&lt;span class="nb"&gt;exec&lt;/span&gt; /QOpenSys/pkgs/bin/python3.9 server.py
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Gotcha 2: one local connection per job.&lt;/strong&gt; Without server mode the PASE CLI driver gives you one local connection per job. That is why the server opens it once and &lt;code&gt;HTTPServer&lt;/code&gt; (not &lt;code&gt;ThreadingHTTPServer&lt;/code&gt;) handles requests one at a time. For an internal tool that is fine; see "Before production" below for the alternatives.&lt;/p&gt;

&lt;p&gt;Autocommit is off on purpose: the API decides when a unit of work ends.&lt;/p&gt;

&lt;h2&gt;
  
  
  Reading rows into JSON
&lt;/h2&gt;

&lt;p&gt;One helper runs a parameterised SELECT and returns a list of dicts:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;rows&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sql&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;params&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;()):&lt;/span&gt;
    &lt;span class="n"&gt;stmt&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;ibm_db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;prepare&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;connection&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="n"&gt;sql&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;try&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;ibm_db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;execute&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;stmt&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;tuple&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;params&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
        &lt;span class="n"&gt;numeric&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;ibm_db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;field_name&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;stmt&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;lower&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
                   &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nf"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ibm_db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;num_fields&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;stmt&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
                   &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;ibm_db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;field_type&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;stmt&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;decimal&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;numeric&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)}&lt;/span&gt;
        &lt;span class="n"&gt;out&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;
        &lt;span class="n"&gt;row&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;ibm_db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fetch_assoc&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;stmt&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="n"&gt;row&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="n"&gt;rec&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;
            &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;row&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;items&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
                &lt;span class="n"&gt;key&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;lower&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
                &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;key&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;numeric&lt;/span&gt; &lt;span class="ow"&gt;and&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="ow"&gt;is&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="bp"&gt;None&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
                    &lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;decimal&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Decimal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;str&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
                &lt;span class="k"&gt;elif&lt;/span&gt; &lt;span class="nf"&gt;isinstance&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
                    &lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;rstrip&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
                &lt;span class="n"&gt;rec&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;
            &lt;span class="n"&gt;out&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;rec&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="n"&gt;row&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;ibm_db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fetch_assoc&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;stmt&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;finally&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;ibm_db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;free_stmt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;stmt&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;ibm_db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;commit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;connection&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;out&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Things an IBM i developer recognises immediately:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;CHAR&lt;/code&gt; columns come back padded.&lt;/strong&gt; &lt;code&gt;rstrip()&lt;/code&gt; every string, or your JSON is full of &lt;code&gt;"OF-0001   "&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Packed and zoned decimals&lt;/strong&gt; arrive as strings or floats depending on the driver version. Converting them to &lt;code&gt;Decimal&lt;/code&gt; keeps money exact; the JSON encoder then writes whole numbers as integers.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Always use parameter markers&lt;/strong&gt; (&lt;code&gt;?&lt;/code&gt;). Query parameters from the URL go into &lt;code&gt;params&lt;/code&gt;, never into the SQL text, so there is no SQL injection.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Free the statement.&lt;/strong&gt; With a server-mode connection an open statement handle can keep the next statement waiting. &lt;code&gt;free_stmt&lt;/code&gt; in &lt;code&gt;finally&lt;/code&gt; avoids a hang that is hard to trace.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A handler is then just SQL with optional filters:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;get_stock&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;q&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;sql&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;SELECT STWHS AS warehouse, STITEM AS item, ITDESC AS description,&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
           &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt; STQTY AS on_hand, ITMINQ AS minimum, STVALUE AS value,&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
           &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt; REORDER AS reorder FROM STOCK_STATUS WHERE 1 = 1&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;params&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;warehouse&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;q&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;sql&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt; AND STWHS = ?&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
        &lt;span class="n"&gt;params&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;q&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;warehouse&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nf"&gt;upper&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;q&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;reorder&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;""&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;upper&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Y&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;sql&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt; AND REORDER = &lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;Y&lt;/span&gt;&lt;span class="sh"&gt;'"&lt;/span&gt;
    &lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;rows&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sql&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt; ORDER BY STWHS, STITEM&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;params&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;count&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nf"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;stock&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;AS warehouse&lt;/code&gt; gives the JSON readable names, while the tables keep their short RPG-style column names.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F9wgm9y0hpkteot8az5fg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F9wgm9y0hpkteot8az5fg.png" alt="GET /api/stock?reorder=Y" width="800" height="485"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Routing without a framework
&lt;/h2&gt;

&lt;p&gt;A list of &lt;code&gt;(method, regex, handler)&lt;/code&gt; tuples is enough:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;ROUTES&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;GET&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;re&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;compile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;r&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;^/api/health$&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="n"&gt;get_health&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;GET&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;re&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;compile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;r&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;^/api/items$&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="n"&gt;get_items&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;GET&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;re&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;compile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;r&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;^/api/items/([^/]+)$&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="n"&gt;get_item&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;GET&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;re&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;compile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;r&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;^/api/stock$&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="n"&gt;get_stock&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;GET&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;re&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;compile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;r&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;^/api/movements$&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="n"&gt;get_movements&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;do_GET&lt;/code&gt; matches the path, passes the query string and the regex groups to the handler, and turns a small &lt;code&gt;ApiError(status, message)&lt;/code&gt; exception into a JSON error. Anything unexpected becomes a &lt;code&gt;500&lt;/code&gt; with the driver message, and the connection is never dropped without a response.&lt;/p&gt;

&lt;h2&gt;
  
  
  Writes: reuse the rules, do not copy them
&lt;/h2&gt;

&lt;p&gt;Posting a stock movement has rules: valid movement type, active item and warehouse, non-zero quantity, no negative stock, and the stock row must be locked so two users cannot take the last units at the same time. The 5250 program already does all of that in RPG.&lt;/p&gt;

&lt;p&gt;Rewriting those rules in Python would give two versions that slowly drift apart. So the rules moved into an SQL procedure, and both channels call it. The business errors use their own SQLSTATE values:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;OR&lt;/span&gt; &lt;span class="k"&gt;REPLACE&lt;/span&gt; &lt;span class="k"&gt;PROCEDURE&lt;/span&gt; &lt;span class="n"&gt;POST_MOVEMENT&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="k"&gt;IN&lt;/span&gt;  &lt;span class="n"&gt;P_TYPE&lt;/span&gt;    &lt;span class="nb"&gt;CHAR&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
  &lt;span class="k"&gt;IN&lt;/span&gt;  &lt;span class="n"&gt;P_ITEM&lt;/span&gt;    &lt;span class="nb"&gt;CHAR&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
  &lt;span class="k"&gt;IN&lt;/span&gt;  &lt;span class="n"&gt;P_WHS&lt;/span&gt;     &lt;span class="nb"&gt;CHAR&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
  &lt;span class="k"&gt;IN&lt;/span&gt;  &lt;span class="n"&gt;P_QTY&lt;/span&gt;     &lt;span class="nb"&gt;DECIMAL&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;9&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
  &lt;span class="k"&gt;IN&lt;/span&gt;  &lt;span class="n"&gt;P_DATE&lt;/span&gt;    &lt;span class="nb"&gt;DATE&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="k"&gt;IN&lt;/span&gt;  &lt;span class="n"&gt;P_REF&lt;/span&gt;     &lt;span class="nb"&gt;CHAR&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;20&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
  &lt;span class="k"&gt;OUT&lt;/span&gt; &lt;span class="n"&gt;P_MVNO&lt;/span&gt;    &lt;span class="nb"&gt;DECIMAL&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;9&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
  &lt;span class="k"&gt;OUT&lt;/span&gt; &lt;span class="n"&gt;P_NEWQTY&lt;/span&gt;  &lt;span class="nb"&gt;DECIMAL&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;9&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
  &lt;span class="k"&gt;LANGUAGE&lt;/span&gt; &lt;span class="k"&gt;SQL&lt;/span&gt;
  &lt;span class="k"&gt;MODIFIES&lt;/span&gt; &lt;span class="k"&gt;SQL&lt;/span&gt; &lt;span class="k"&gt;DATA&lt;/span&gt;
&lt;span class="k"&gt;BEGIN&lt;/span&gt;
  &lt;span class="p"&gt;...&lt;/span&gt;
  &lt;span class="n"&gt;IF&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="k"&gt;EXISTS&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;ITEM_MASTER&lt;/span&gt;
                  &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;ITITEM&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;P_ITEM&lt;/span&gt; &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="n"&gt;ITSTAT&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'A'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;THEN&lt;/span&gt;
    &lt;span class="n"&gt;SIGNAL&lt;/span&gt; &lt;span class="k"&gt;SQLSTATE&lt;/span&gt; &lt;span class="s1"&gt;'75002'&lt;/span&gt;
      &lt;span class="k"&gt;SET&lt;/span&gt; &lt;span class="k"&gt;MESSAGE_TEXT&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'Item does not exist or is inactive'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;END&lt;/span&gt; &lt;span class="n"&gt;IF&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;...&lt;/span&gt;
&lt;span class="k"&gt;END&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The procedure does &lt;strong&gt;not&lt;/strong&gt; commit. The caller owns the unit of work, which is exactly what an API needs: commit on success, roll back on any error.&lt;/p&gt;

&lt;p&gt;On the Python side, the SQLSTATE becomes the HTTP status:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;BUSINESS_ERRORS&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;75001&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;400&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;75002&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;404&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;75003&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;404&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;75004&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;400&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;75005&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;400&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;75006&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;409&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;   &lt;span class="c1"&gt;# 75006 = insufficient stock
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;try&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;ibm_db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;callproc&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;conn&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;SCHEMA&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;.POST_MOVEMENT&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                             &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;mtype&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;item&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;whs&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;qty&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;date&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;ref&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                              &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt; &lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;9&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt; &lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;9&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
    &lt;span class="n"&gt;stmt&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="n"&gt;number&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;on_hand&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;7&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="n"&gt;ibm_db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;commit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;conn&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;except&lt;/span&gt; &lt;span class="nb"&gt;Exception&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;exc&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;state&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;ibm_db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;stmt_error&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="ow"&gt;or&lt;/span&gt; &lt;span class="sh"&gt;""&lt;/span&gt;        &lt;span class="c1"&gt;# read it BEFORE the rollback
&lt;/span&gt;    &lt;span class="n"&gt;message&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;ibm_db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;stmt_errormsg&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="ow"&gt;or&lt;/span&gt; &lt;span class="nf"&gt;str&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;exc&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;ibm_db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;rollback&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;conn&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;status&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;BUSINESS_ERRORS&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;state&lt;/span&gt;&lt;span class="p"&gt;[:&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="mi"&gt;500&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="bp"&gt;...&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Gotcha 3: read the error before you roll back.&lt;/strong&gt; &lt;code&gt;rollback()&lt;/code&gt; clears the statement error, and you are left with an empty SQLSTATE and a useless &lt;code&gt;500&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Gotcha 4: OUT parameters.&lt;/strong&gt; &lt;code&gt;callproc&lt;/code&gt; returns a tuple: the statement first, then every parameter, so the two OUT values are items 7 and 8. The driver sizes the OUT buffer from the initial value you pass, and it does not clear what follows the returned digits. Passing nine blanks for a &lt;code&gt;DECIMAL(9, 0)&lt;/code&gt; and keeping only the digits afterwards gives a clean number:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;out_number&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;digits&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;re&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sub&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;r&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;[^0-9-]&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;""&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;str&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;int&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;digits&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;digits&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="bp"&gt;None&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The result is the behaviour a client developer expects:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;POST /api/movements  {"type":"O","item":"OF-0001","warehouse":"CTR","quantity":5,"reference":"DO-1"}
201  {"number": 18, "item": "OF-0001", "warehouse": "CTR", "type": "O",
      "quantity": 5, "date": "2026-09-26", "on_hand": 320}

POST /api/movements  {"type":"O","item":"EL-0005","warehouse":"CTR","quantity":999}
409  {"error": "Insufficient stock for this movement"}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F0hx0kqlkjvjnubjnaly7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F0hx0kqlkjvjnubjnaly7.png" alt="POST with insufficient stock returns 409" width="800" height="546"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Running it
&lt;/h2&gt;

&lt;p&gt;PUB400 does not open extra ports to the internet, so the server listens on &lt;code&gt;127.0.0.1&lt;/code&gt; and an SSH tunnel brings it to the PC. One command starts both:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;ssh &lt;span class="nt"&gt;-t&lt;/span&gt; &lt;span class="nt"&gt;-p&lt;/span&gt; 2222 &lt;span class="nt"&gt;-L&lt;/span&gt; 48400:127.0.0.1:48400 USER@pub400.com &lt;span class="se"&gt;\&lt;/span&gt;
    /QOpenSys/pkgs/bin/bash /home/USER/inventory/api/start_api.sh
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then open &lt;code&gt;http://localhost:48400/api/health&lt;/code&gt; in a browser, or run the &lt;code&gt;curl&lt;/code&gt; smoke test in &lt;code&gt;api/test_api.sh&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;One more small help: the request handler arms &lt;code&gt;faulthandler.dump_traceback_later(20)&lt;/code&gt;. If a request is still running after 20 seconds, Python prints its stack to the log, so a database wait shows the exact line instead of a silent hang.&lt;/p&gt;

&lt;h2&gt;
  
  
  Before production
&lt;/h2&gt;

&lt;p&gt;This is a sample, and I kept it small on purpose. For a real system I would add:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Authentication and TLS&lt;/strong&gt;, for example behind the IBM HTTP Server (Apache) as a reverse proxy, instead of exposing &lt;code&gt;http.server&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Concurrency&lt;/strong&gt;: a WSGI server (gunicorn, waitress) with server-mode connections or one connection per worker, instead of one request at a time.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;A proper job&lt;/strong&gt;: start it with &lt;code&gt;SBMJOB&lt;/code&gt; in its own subsystem or with Service Commander, so it runs under a service profile and restarts with the system.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Logging and monitoring&lt;/strong&gt; of response times and &lt;code&gt;500&lt;/code&gt;s.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The core idea stays the same: Python in PASE is a thin HTTP layer, and Db2 for i (tables, views and SQL procedures) keeps the data and the rules.&lt;/p&gt;

&lt;h2&gt;
  
  
  Takeaways
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Python 3.9 and &lt;code&gt;ibm_db&lt;/code&gt; are one &lt;code&gt;yum install&lt;/code&gt; away on any current IBM i.&lt;/li&gt;
&lt;li&gt;Trim &lt;code&gt;CHAR&lt;/code&gt;, keep decimals as &lt;code&gt;Decimal&lt;/code&gt;, and always use parameter markers.&lt;/li&gt;
&lt;li&gt;Put the business rules in an SQL procedure and call it from every channel, the green screen and the API alike.&lt;/li&gt;
&lt;li&gt;Map your own SQLSTATE values to HTTP status codes, and read the error before the rollback.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The full source, the SQL scripts and the 5250 side of the application are on GitHub: &lt;strong&gt;&lt;a href="https://github.com/dodikibmi/ibmi-inventory" rel="noopener noreferrer"&gt;github.com/dodikibmi/ibmi-inventory&lt;/a&gt;&lt;/strong&gt;. Questions and corrections are welcome in the comments.&lt;/p&gt;




&lt;p&gt;&lt;em&gt;I am an IBM i developer from Indonesia, working with RPG, SQL and CL on Db2 for i. This project is a sample application I built on PUB400 to practise modern IBM i techniques in public.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>python</category>
      <category>db2</category>
      <category>api</category>
      <category>ibmi</category>
    </item>
  </channel>
</rss>
