/
Tip 5: How to set entire body from input parms and get entire response into output parms (in low-code plugin Consume REST Service)

Tip 5: How to set entire body from input parms and get entire response into output parms (in low-code plugin Consume REST Service)



In a low-code module that uses the plugin Consume REST Service, you do NOT have to set individual input fields in the "body", or set individual output fields from the "response", if the structures of the input parameters and output parameters match exactly. You just need to make sure the "properties" of the "input" and "output" objects match with what's specified in the the REST API.

Below is an example.

Use the code below to create JS file pjstips_05_1.js as a web service, with POST method, with URL http://localhost:8081/run/pjstips/pjstips_05_1. Refer to here on how to do that.

pjstips_05_1.js
function app(req, res) { var firstName = req.body.firstName; var lastName = req.body.lastName; var fullName = `${firstName.trim()} ${lastName.trim()}`; var data = { "firstName" : firstName, "lastName" : lastName, "fullName" : fullName }; res.json(data); } exports.run = app;



This REST API expects a body as JSON in the format of:

{ "firstName": "xxx" "lastName" : "yyy" }



This REST API will return a JSON object in the format of:

{ "firstName": "xxx" "lastName" : "yyy" "fullName" : "xxx yyy" }



Use the code below to create a module file pjstips_05_2.module.json. Refer to here on how to do that.

pjstips_05_2.module.json
{ "routines": [ { "name": "routine1", "inputs": [ { "type": "object", "ibmiLength": 10, "name": "dataIn", "children": [ { "type": "string", "ibmiLength": 30, "name": "firstName" }, { "type": "string", "ibmiLength": 30, "name": "lastName" } ] } ], "outputs": [ { "type": "object", "ibmiLength": 10, "name": "dataOut", "children": [ { "type": "string", "ibmiLength": 30, "name": "firstName", "children": [] }, { "type": "string", "ibmiLength": 30, "name": "lastName" }, { "type": "string", "ibmiLength": 50, "name": "fullName" } ] } ], "steps": [ { "text": "Consume REST service", "answers": { "plugin": "Custom:web-service", "uri": "http://localhost:8081/run/pjstips/pjstips_05_1", "method": "POST", "headers": "", "body": "input[\"dataIn\"]", "json": true, "auth": "", "capture_response": true, "capture_on_error": false, "capture_as": "Value", "specific_property": "", "destination": "Module output", "module_output": "output[\"dataOut\"]" } } ] } ] }



The input parameters are defined like this. The object "dataIn" should match with the structure as expected in the "body".



The ouput parameters are defined like this. The object "dataOut" should match with the structure of the JSON object as returned from the REST API.



The plugin Consume REST Service is used like this:





If you click on the </> button ("Switch routine to Node.js code"), you'll see the "real" PJS code below, to see how it works. Variable _data, which is a JSON object, is returned from the REST API. Then it's assigned to the property "dataOut" of the "output" object. So if the properties of the returned JSON object matches with the "subfields" of parameter "dataOut", it should work OK.

// Consume REST service var _data = pjs.sendRequest({ method: "POST", uri: `http://localhost:8081/run/pjstips/pjstips_05_1`, body: input["dataIn"], json: true }); output["dataOut"] = _data;



Here's the RPG code to call the low-code module to test it out.

PJSTIPS052.rpgle
**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; Dcl-DS dataIn; firstName VarChar(30); lastName VarChar(30); End-DS; End-DS; Dcl-DS OutputDS Qualified Inz; Dcl-DS dataOut; firstName VarChar(30); lastName VarChar(30); fullName VarChar(50); End-DS; End-DS; InputDS.dataIn.firstName = 'John'; InputDS.dataIn.LastName = 'Smith'; Monitor; PJSCALL('*MODULE' : 'pjstips:pjstips_05_2.module.json' : 'routine1' : InputDS : %Size(InputDS) : OutputDS : %Size(OutputDS) ); On-Error; EndMon; dsply OutputDS.dataOut.firstName; dsply OutputDS.dataOut.lastName ; dsply OutputDS.dataOut.fullName ; *inlr = *on; return;