How to make RPGLE as a Web Services (2/2)
using itoolkitjs to make endpoint as a web service
In this section, we will create a web service endpoint using ExpressJS and itoolkit to interact with the RPGLE program on AS400 (IBM iSeries). We will cover the steps to install requirements, set up the web server, integrate with AS400, and configure Postman for testing.
Step-by-Step Creation of JS with ExpressJS
After creating the RPGLE program on AS400 (IBM iSeries), the next step is to create a web service endpoint using ExpressJS and itoolkit. This will allow us to interact with the RPGLE program via RESTful API calls.
1. Install Requirements
First, ensure you have Node.js and npm installed on your machine. Then, create a new directory for your project and initialize it with npm:
mkdir as400-web-service
cd as400-web-service
npm init -yNext, install the necessary packages:
npm install express itoolkit fast-xml-parser2. Create Web Server
Create a file named caller.js and set up the basic Express server:
const express = require('express');
const { Connection, ProgramCall } = require('itoolkit');
const { XMLParser } = require('fast-xml-parser');
const app = express();
const port = 3000; // Bisa diganti sesuai kebutuhan
// Middleware untuk parsing JSON
app.use(express.json());
// Membuat koneksi ke AS400
const connection = new Connection({
transport: 'ssh',
transportOptions: {
host: 'pub400.com',
port: 2222,
username: 'USERNAME',
password: 'URPASSWORD',
},
});
// Endpoint untuk memanggil JDWLMAIN program
app.post('/addcustomer', (req, res) => {
const receiver = {
name: 'WSMAIN',
type: 'ds',
fields: [
{ name: 'iid', type: '10A', value: req.body.iid || '' }, // Default value jika tidak diberikan
{ name: 'iname', type: '10A', value: req.body.iname || '' },
{ name: 'iaddress', type: '10A', value: req.body.iaddress || '' },
{ name: 'ores', type: '2A', value: '' }, // Output field, initialize as empty
{ name: 'odesc', type: '50A', value: '' } // Output field, initialize as empty
],
};
const command = new ProgramCall('WSMAIN', { lib: 'YOURLIB' });
command.addParam(receiver);
connection.add(command);
// Menjalankan command
connection.run((error, xmlOutput) => {
if (error) {
return res.status(500).json({ error: 'Error executing AS400 command', details: error });
}
// Parsing XML output
const parser = new XMLParser();
const result = parser.parse(xmlOutput);
// Mengirimkan response dalam bentuk JSON
res.json(result);
});
});
// Menjalankan server
app.listen(port, () => {
console.log(`Server listening at http://localhost:${port}`);
});3. Integration to AS400 Server
In the caller.js file, we have already set up the connection to AS400 using itoolkit. Here’s a breakdown of the integration:
- Connection Setup:
const connection = new Connection({
transport: 'ssh',
transportOptions: {
host: 'pub400.com',
port: 2222,
username: '',
password: '',
},
});- Program Call:
const command = new ProgramCall('WSMAIN', { lib: 'URLIB' });
command.addParam(receiver);- Running The Command:
connection.run((error, xmlOutput) => {
if (error) {
return res.status(500).json({ error: 'Error executing AS400 command', details: error });
}
const parser = new XMLParser();
const result = parser.parse(xmlOutput);
res.json(result);
});4. Postman Setup
To test the web service endpoint, you can use Postman. Here’s how to set it up:
- Open Postman and create a new request.
- Set the request type to
POST. - Enter the URL for your endpoint, e.g.,
http://localhost:3000/addcustomer. - Set the request body to
JSONand provide the necessary input data:
{
"iid": "0000000001",
"iname": "Satria Tama",
"iaddress": "Jakarta"
}5. Response From Postman
Get Satriatama’s stories in your inbox
Join Medium for free to get updates from this writer.
After Send the request you’ll get the response from the server.
{
"?xml": "",
"myscript": {
"pgm": {
"parm": {
"ds": {
"data": [
1,
"Satria Tam",
"Jakarta",
1,
"Data Successfully Updated"
]
}
},
"success": "+++ success MUSTOFA1 WSMAIN "
}
}
}Conclusion
By following these steps, you have created a web service endpoint using ExpressJS and itoolkit that interacts with an RPGLE program on AS400. This setup allows you to leverage the capabilities of AS400 while using modern web technologies. If you have any questions or need further assistance, feel free to ask!






