Popular Posts

IBM Datapower GatewayScript


IBM API Assembly Language


GatewayScript is a new transformation technology for API, Web, and Mobile and is available in DataPower. GatewayScript is based on the JavaScript programming language. More precisely, it supports the ECMAScript 5.1 version of the specification. On the DataPower appliance, JavaScript runs in ‘strict’ mode only. The strict mode provides a more consistent and more disciplined behavior.

For example, if a script attempts to write a property of a DataPower API object and that object is frozen, an immediate exception is thrown. The ‘strict’ mode behavior supports stronger error checking and handling and increases security.

DataPower Playground (GatewayScript Fiddle)

IBM DataPower Playground is an interactive website that lets you write GatewayScript code and execute it on a cloud-hosted DataPower Gateway for learning purposes. Available at : https://datapower-playground.mybluemix.net/

GatewayScript provides a series of libraries that give the developer access to specific APIs. All access to DataPower facilities is provided through these APIs.

IBM DataPower has below important modules and access to these modules can be done with nodeJS module import syntax.

i.e., var module=require('moduleName');

Following are some important modules and their usage

  1. Metadata Headers

  2. Console Logging

  3. Utilities

  4. Testing/Assertions

  5. URL Opener

  6. Service Metadata

Other Implicit Objects

  • Session - useful to read input data as JSON,Buffer etc.

  • APIM - This works only in IBM DataPower Gateway but not in Datapower Playground. This object useful to send response back to calling service

Examples of the above modules

Metadata Headers

This module used to read header information. The headers module is broken down into three sub-modules:

  • Original: Represents the original collection of headers received off the wire. For a request rule, these are the headers received in the request from the client. For a response rule, these are the headers received in the response from the remote, target host. Original headers are read-only.

  • Current: Represents the current state of the headers as they are being passed from action to action along with a processing rule. The processing rule may be either a request rule or a response rule. Current headers are read/write.

  • Response: Represents the header collection that is used for a response that is being accessed in the request rule. This is used for a loopback firewall or when skip-backside is used in a multi-protocol gateway. Response headers are read/write.

var hm=require('header-metadata');


The console module is provided without having to use the require('console') statement.

This module is automatically attached to the Global object.

ar hm=require('header-metadata');


APIM is an implicit object available in DataPower Gateway

Below is the example to read query parameters from the input source and converting into string format using 'querystring' module.

var qs = require ('querystring');
 var reqp = apim.getvariable('request.parameters');
 var accountId=reqp['accountId'];
  //preparing queryString
  var params = {}
  for (var name in reqp) {
          params[name] = reqp[name];
  var queryString=qs.stringify(params);


This URL Open module used to connect to services and reading the response in a callback method.

var urlopen = require('urlopen');
var hm = require('header-metadata') ;
var ibmUser = hm.current.get("Authorization") ;
console.log('session.parameters :'+session.parameters);
var my_content = hm.current.get("Content-type") ;
var options = {
            target: 'http://server.com',
            method: 'GET',
           headers: { 'Authorization' : ibmUser  },
       contentType: 'application/x-www-form-urlencoded',
           timeout: 60};

urlopen.open(options, function(error, response) {
  if (error) {
    // an error occurred during the request sending or response header parsing
    session.output.write("urlopen error: "+JSON.stringify(error));
  } else {
    // get the response status code
    var responseStatusCode = response.statusCode;
    var responseReasonPhrase = response.contentType;
    console.log("Response statusCode: " + responseStatusCode);
    console.log("Response reason phrase: " + responseReasonPhrase);
    // reading response data
    response.readAsJSON(function(err, json) {
        if (err) throw err;
        // Update the transactional payload to that just fetched
            apim.setvariable('message.body', json.Capability);
        console.info("Response Sent Successfully");

Below example describes reading input stream from preceding component

apim.readInputAsJSON(function(err, json) {
        if (err) throw err;
        // Update the transactional payload to that just fetched
            apim.setvariable('message.body', json.Customer);
        console.info("Response Sent Successfully");

session object represents session of GatewayScript action. This is an implicit object and no import required.

session.input.readAsJSON(function (error, jsonObject) {
           console.info("readAsJSON success: %s", jsonObject);
           console.info("Read/Write Payload Demo Complete");

Service Metadata module used to read service variables

var sm = require('service-metadata');
var list = sm.list();

1 comment:

  1. Hi, I want to capture payload without headers in data power..Please help me.