Giter Club home page Giter Club logo

blog's People

Contributors

rainerross avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

blog's Issues

RPG call Node.js with simple or complex parameters

Since several years I'm developing web applications with my IBM i look at MyApp. To make the communication easier between RPG and the HTTP-server I created the WEBSRVUTL

I use JavaScript at frontend for web applications and I think JavaSript at backend called Node.js is a really good thing and the best is it's OpenSource and easy to learn. Look at this simple example to send SMS messages from RPG to Node.js Send SMS with Twilio

When you are a RPG programmer you are interested to call Node.js programs or call RPG programs from Node.js.

You need

  • Node.js version 10 or higher - currently Node.js v18
  • Install Node.js with Access Client Solutions Open Source Package Management or with "yum install nodejs18"

Install and run Node.js programs on your IBM i

The best way to communicate between RPG and Node.js is the JSON protocol

When you want diving deeper look at the article from Birgitta Hauser SQL JSON publishing functions

RPG call Node.js

the easiest way to call a Node.js program from RPG is a REST webservice. The best way for simple or complex parameters are POST requests.

RPG call Node.js performance

the average runtime is 10 milliseconds

The Node.js part

Node.js program getting HTTP POST requests link to sourcecode

const http = require('http');
const port = 8080;

let response = {
	success: true,
	error:	 ''
}		

function myFunction(data) {		// Put your code in this function
	console.log('Data: ' + JSON.stringify(data));
}

http.createServer(function(req, res) {
	console.log('URL: '		+ req.url);
	console.log('Method: '		+ req.method);
	console.log('Content-Type: '	+ req.headers['content-type']);
	console.log('Content-Length: '	+ req.headers['content-length']);
	
	if (req.method === 'POST') {
		let body = new Array();
		req.on('data', function(data) {
			body.push(data);
		})
		req.on('end', function() {
			let data = JSON.parse(Buffer.concat(body).toString());
			myFunction(data);
			res.writeHead(200, {'Content-Type': 'application/json'});
			res.end(JSON.stringify(response));
		})
	}
}).listen(port);

console.log('Server running at Port: ' + port);

Test it with SQL

SQL for testing HTTP POST requests with simple parameters

the port must be the same like the Node.js program

values QSYS2.HTTP_POST(
  'http://IBM i IP-address:8080/mycall',
  json_object(
   'from'   value '01719637923',
   'to'     value '08941325294',
   'text'   value 'My Message to you!'
  ),
  json_object(
   'header' value 'Content-Type,application/json; charset=utf-8'
  )
);

Simple JSON data

{
   "from":"01719637923",
   "to":"08941325294",
   "text":"My Message to you!"
}

When you start your Node.js program and run the SQL you will see this

Server running at Port: 8080                                                     
URL: /mycall                                                                    
Method: POST                                                                     
Content-Type: application/json; charset=utf-8                                    
Content-Length: 69                                                               
Data: {"from":"01719637923","to":"08941325294","text":"My Message to you!"}   

SQL for testing HTTP POST requests with complex parameters

values QSYS2.HTTP_POST(
  'http://IBM i IP-address:8080/mycall',
  json_object(
   'customer' value json_object(
     'firstname' value 'Rainer',
     'lastname'  value 'Ross'
   ),   
   'data'  value  json_array(
     json_object(
     'id'       value 5,
     'name'     value 'Thomas',
     'quantity' value 2560.25         
    ),
     json_object(
     'id'       value 6,
     'name'     value 'Maria',
     'quantity' value 5689.50         
    )       
   ),
   'phone'  value '+49465798791313',
   'text'   value 'My Message to you!'
  ),
  json_object(
   'header' value 'Content-Type,application/json; charset=utf-8'
  )
);

Complex JSON data

{
      "customer": {
         "firstname": "Rainer",
         "lastname": "Ross"
      },
      "data":[
	      {
	         "id": 5,
	         "name": "Thomas",
	         "quantity": 2560.25
	      },
	      {
	         "id": 6,
	         "name": "Maria",
	         "quantity": 5689.50
	      }
       ],
       "phone": "+49465798791313",
       "text": "My Message to you!"
}

The RPG part

RPG program for HTTP POST requests with simple parameters link to sourcecode

         ctl-opt main(main) dftactgrp(*no);
      //------------------------------------------------------------------//
      //                                                                  //
      // Send SMS with TWILIO                                             //
      //                                                                  //
      //-----------------                                                 //
      // R.Ross 07.2022 *                                                 //
      //------------------------------------------------------------------//
      // SQL-Options                                                      //
      //------------------------------------------------------------------//

         exec sql set option datfmt=*iso, timfmt=*iso, commit=*none,
                             decmpt=*period, closqlcsr=*endactgrp;

      //------------------------------------------------------------------//
      // Datastructure SMS                                                //
      //------------------------------------------------------------------//

         dcl-ds  DsSMS       qualified inz;
                  from       varchar(30);             // SMS-from
                  to         varchar(30);             // SMS-to
                  text       varchar(256);            // SMS-text
         end-ds;

      //------------------------------------------------------------------//
      // Main                                                             //
      //------------------------------------------------------------------//
         dcl-proc main;

         dcl-s   LocUrl      varchar(90);
         dcl-s   LocHeader   varchar(128);
         dcl-s   LocData     varchar(1000);
         dcl-s   LocResponse varchar(128);

           LocUrl     = 'http://IBM i IP:8080/sendsms';

           exec sql
            set :LocHeader = JSON_OBJECT(
                'header' value 'Content-Type,application/json; charset=utf-8'
            );

           DsSMS.from = '+49678901234';
           DsSMS.to   = '+49171456789';
           DsSMS.text = 'My Message to you!';

           LocData    = crtJson(DsSMS);

           exec sql
            set :LocResponse = QSYS2.HTTP_POST(:LocUrl, :LocData, :LocHeader);

         end-proc;
      //------------------------------------------------------------------//
      // create JSON data                                                 //
      // {"from":"+123456789", "to":"+123456789" , "text":"My Message"}   //
      //------------------------------------------------------------------//
         dcl-proc crtJson;
         dcl-pi *n           like(LocData);
                 PiSMS       likeds(DsSMS) const;
         end-pi;

         dcl-s   LocData     varchar(1000);           // JSON-Data

           exec sql
            set :LocData = JSON_OBJECT(
               'from'    value trim(:PiSMS.from),
               'to'      value trim(:PiSMS.to),
               'text'    value trim(:PiSMS.text)
           );

           return LocData;

         end-proc;
      //------------------------------------------------------------------// 

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.