Saltar al contenido

IBM DataPower – MPG – GatewayScript – Invoke Backend REST – routing-url http status code

Code GatewayScript

var urlopen = require ('urlopen');
var hm = require('header-metadata');
var sm = require ('service-metadata');

var url_target = sm.getVar('var://service/routing-url');

// define the urlopen options
var options = {
   target: url_target,
   sslClientProfile: '<ssl-client-profile>',
   method: 'get',
   contentType: 'application/json',
   timeout: 60
};

// open connection to target and send data over
urlopen.open (options, function (error, response) {
   if (error) {
      // an error occurred during request sending or response header parsing
      session.output.write ("urlopen connect error: " + JSON.stringify(error));
   } else {
      // read response data
      // get the response status code
      var responseStatusCode = response.statusCode;
      if (responseStatusCode == 200) {
         response.readAsBuffer(function(error, responseData) {
            if (error) {
               // error while reading response or transferring data to Buffer
               session.output.write("readAsBuffer error: " + JSON.stringify(error));
            } else {
               var data_response = JSON.parse(responseData.toString());
               if(data_response.flag== 'true'){
                  hm.response.statusCode = "204";
               }else{
                   hm.response.statusCode = "50x";
               }
            } 
         });
      }else {
         session.output.write ("urlopen target return statusCode " + responseStatusCode);
      }
   }
}); // end of urlopen.open()
hm.response.set('content-type', 'application/json');

Si te ha interesado este artículo y deseas un apoyo o asesoría en algún requerimiento, envíame un mensaje a: (info@juliopari.com) o sino a través de Linkedin: https://www.linkedin.com/in/juliopari/