This tip documents how to:
- Create a low-code module to consume a REST API.
- Call the low-code module from an RPG program, by using the PJSCALL program.
Create a low-code module to consume a REST API
- Navigate to the workspace "pjstips".
- Click New/Module File.
Click Edit/Source, to bring up the Source view.
- Clear the current default code, and enter the code below.
Code Block language js title pjstips_03_1.module.json { "routines": [ { "name": "routine1", "inputs": [ { "type": "decimal", "ibmiLength": 4, "name": "custno", "ibmiDecimals": 0 } ], "outputs": [ { "type": "string", "ibmiLength": 30, "name": "name" } ], "steps": [ { "text": "Consume REST service", "answers": { "plugin": "Custom:web-service", "uri": "http://localhost:8081/run/pjstips/pjstips_02_3", "method": "POST", "headers": "", "body": "{\n \"custno\": input[\"custno\"]\n}", "json": true, "auth": "", "capture_response": true, "capture_on_error": false, "capture_as": "Value", "specific_property": "", "destination": "Work variable", "work_variable": "myres" } }, { "text": "Set Module output", "answers": { "plugin": "Program Data:set-module-output", "module-output-values": { "name": "myres[\"name\"]" } } } ] } ] }
- Click Edit/Design, to bring up the Design view.
- If needed, in the "Consume REST service" step, change the Endpoint URL to point to the correct host/port.
- Click Home/Save As, and save as pjstips_03_1.module.json.
- You've created a low-code module, as shown below, with a routine named "routine1" that:
- Takes an input parameter of "custno".
- Uses plug plugin "Consume REST service" to issue a POST request to URL http://localhost:8081/run/pjstips/pjstips_02_3 (created in a previous example).
- The input "custno" is specified in the "body" of the POST request.
- The returned JSON object from the REST web service is saved in a work variable named "myres".
- The value of myres["name"] is used to set the output value output["name"].
Call the low-code module from an RPG program
- You can call this low-module from another low-code module, from another "regular" (i.e. NOT low-code) PJS module, or from an RPG program. To call it from an RPG program, use the code generated in the panel "IBM i Call Interface" panel.
- Below is an example, where the input custno is set to 1234 to pass to the low-code module, which calls the REST service to get the customer name, which is then returned to the RPG program, which is then displayed on the screen.
Code Block | ||||
---|---|---|---|---|
| ||||
**free
Dcl-PR PJSCALL ExtPgm;
ParmType Char(30) Const;
ModuleID VarUCS2(500) Const;
RoutineName VarUCS2(70) Const;
InputParms Char(16773104) Const Options(*Varsize:*Omit:*NoPass);
InputParmSize Int(10) Const Options(*Omit:*NoPass);
OutputParms Char(16773104) Options(*Varsize:*Omit:*NoPass);
OutputParmSize Int(10) Const Options(*Omit:*NoPass);
End-PR;
Dcl-DS InputDS Qualified Inz;
custno Zoned(4: 0);
End-DS;
Dcl-DS OutputDS Qualified Inz;
name VarChar(30);
End-DS;
InputDS.custno = 1234; // pass input to the low-code module in "InputDS"
Monitor;
PJSCALL('*MODULE'
: 'pjstips:pjstips_03_1.module.json'
: 'routine1'
: InputDS
: %Size(InputDS)
: OutputDS
: %Size(OutputDS)
);
On-Error;
EndMon;
dsply OutputDS.name; // output from the low-code module in "OutputDS"
*inlr = *on;
return;
|