API Connect

API Connect

Join this online group to communicate across IBM product users and experts by sharing advice and best practices with peers and staying up to date regarding product enhancements.

 View Only

Handling XML response with GatewayScript

By Leandro Takeda posted Tue September 17, 2019 07:09 PM

  

By Leandro Takeda, 2019, Sep 18

 

Hi,

In the other day I needed to handle XML responses on API Connect. My API was an integration between API Connect and the backend, a SOAP Service.

I created an API to simple expose the same service as JSON.

The backend responded in a structure XML as:

<GetOrdersByConsumptionPlaceOut>

<body>

<Code>01</Code>

      </body>

</GetOrdersByConsumptionPlaceOut>

So, my code just check if that value 01, exist, not matter what value is (it was my scenario) and thrown and error back, in this case, HTTP 500.

var transform = require('transform');

var domTree = apim.getvariable('GetOrdersByConsumptionPlaceOut.body');

var options = { 'expression': 'string(//Code/text())',

'xmldom': domTree

};

//setting the response header

apim.setvariable('message.headers.content-type', 'application/json', 'set');

transform.xpath(options, function(err, xmlNodeList) {

//if the structure exist, throw error

if (err) {

apim.error('MyError', 500, "Server Error", "Something went wrong. Please contact EDP support.");

} else {

 

//if no structure, reply back the response

apim.setvariable('validResponse', ''+xmlNodeList+'');

}

});

The code is self-explained, pretty simple, but if you have any question, let me know.

Thanks!

0 comments
32 views

Permalink