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



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.



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

PJSTIPS052.rpgle