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
  • 1.  How to parse SOAP Response in API Connect gateway script

    Posted Mon March 13, 2023 07:00 AM

    Hi All...

    I am trying to parse the SOAP response in API Connect(10.0.5.2) assembly using (Gateway script) and get the value of SOAP field in variable(consentID) and set it in API HTTP response, but I am not able to parse the SOAP response fields. Please help on implementing the same in Gateway script. 

    SOAP Response
    =============
    <soapenv:Envelope xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
       <soapenv:Body>
          <NS1:GetConsentResponse xmlns:NS1="http://www.infosys.com/response/GetConsent">
             <NS1:CONSENT_ID>12345</NS1:CONSENT_ID>
             <NS1:CONSENT_EXPIRY>2023-03-09T12:25:31.298286</NS1:CONSENT_EXPIRY>
          </NS1:GetConsentResponse>
       </soapenv:Body>
    </soapenv:Envelope>

    Gateway script code
    ===================
    var apim = require('apim');
    var body = apim.getvariable('request.body');
    var xmlString = XML.stringify(body);


    console.error("Root", xmlString);

    var domTree = undefined;
    try {
        // use XML.parse() to parse the xmlString into a DOM tree structure
        domTree = XML.parse(xmlString);
    } catch (error) {
        // there was an error while parsing the XML string
        console.error('error parsing XML string ' + error);
        throw error;
    }

    var transform = require('transform');


    var consentID  = body.getElementsByTagName("Body").item(0).getElementsByTagName("GetConsentResponse").item(0).getElementsByTagName("CONSENT_ID").item(0).textContent;

    //var consentID  = '12345';
    var consentExpiry  = '12345';

    var metadata = {"CONSENT_ID": consentID};
    var resp_string = JSON.stringify (metadata);
    context.message.statusCode = '200 Wonderful';

    apim.setvariable('message.headers.X-API-OAUTH-METADATA-FOR-ACCESSTOKEN', resp_string, 'set');

    apim.setvariable('message.headers.X-API-OAUTH-METADATA-FOR-PAYLOAD', resp_string, 'set');



    ------------------------------
    Ashok Beshra
    ------------------------------


  • 2.  RE: How to parse SOAP Response in API Connect gateway script

    Posted Mon March 13, 2023 08:57 AM

    Hi Ashok,
    You need to use the getElementsByTagNameNS function when working with an element that has a namespace uri associated with it.  The getElementsByTagName should only be used when the element does not have a namespace uri associated with it. Please try

    var consentID  = body.getElementsByTagNameNS("http://schemas.xmlsoap.org/soap/envelope/", "Body").item(0).getElementsByTagNameNS("http://www.infosys.com/response/GetConsent","GetConsentResponse").item(0).getElementsByTagNameNS("http://www.infosys.com/response/GetConsent", "CONSENT_ID").item(0).textContent;
    

    One other word of advice is that you should break this statement up into multiple statements otherwise you could get back an empty nodelist where item(0) is undefined.   Then the following getElementsByTagNameNS will throw an exception, something like getElementByTagNameNS does not exist on undefined

    Best Regards,

    Steve



    ------------------------------
    Steve Linn
    Senior Consulting I/T Specialist
    IBM
    ------------------------------



  • 3.  RE: How to parse SOAP Response in API Connect gateway script

    Posted Mon March 13, 2023 12:26 PM

    Hi Steve...

    Thanks for your valuable feedback on this. I will try the above code and see if it works. As per your advice can I try the below code to get the value of fields from SOAP response? Please suggest. Thanks

    var soapBody = body.getElementsByTagNameNS("http://schemas.xmlsoap.org/soap/envelope/", "Body").item(0);
    var gcResponse = soapBody.getElementsByTagNameNS("http://www.infosys.com/response/GetConsent","GetConsentResponse").item(0);
    var consentID = gcResponse.getElementsByTagNameNS("http://www.infosys.com/response/GetConsent", "CONSENT_ID").item(0).textContent;



    ------------------------------
    Ashok Beshra
    ------------------------------



  • 4.  RE: How to parse SOAP Response in API Connect gateway script

    Posted Mon March 13, 2023 02:25 PM

    Hi Ashok,
    You you should check each value before continuing to use it, and that would include the consentID element.  The item(0) function would return an undefined for an empty nodeset, so you should ensure that the element exists before attempting to search inside of it or as is the case with the CONSENT_ID element, before pulling the text content.

    var consentID;
    var soapBody = body.getElementsByTagNameNS("http://schemas.xmlsoap.org/soap/envelope/", "Body").item(0);
    if (soapBody !== undefined) {
      var gcResponse = soapBody.getElementsByTagNameNS("http://www.infosys.com/response/GetConsent","GetConsentResponse").item(0);
      if (gcResponse !== undefined) {
        var consentIDElement = gcResponse.getElementsByTagNameNS("http://www.infosys.com/response/GetConsent", "CONSENT_ID").item(0);
        if (consentIDElement !== undefined) {
          consentID = consentIDElement.textContent;
        }
      }
    }

    Best Regards,
    Steve

     



    ------------------------------
    Steve Linn
    Senior Consulting I/T Specialist
    IBM
    ------------------------------



  • 5.  RE: How to parse SOAP Response in API Connect gateway script

    Posted Tue March 14, 2023 07:25 AM

    Thanks for your suggestion and guidance Steve. The below code works fine.

    var apim = require('apim');
    var body = apim.getvariable('message.body');
    var xmlString = XML.stringify(body);


    console.error("Root", xmlString);

    var domTree;
    try {
        // use XML.parse() to parse the xmlString into a DOM tree structure
        domTree = XML.parse(xmlString);
    } catch (error) {
        // there was an error while parsing the XML string
        console.error('error parsing XML string ' + error);
        throw error;
    }

    var transform = require('transform');


    var consentID, consentExpiry;
    var soapBody = domTree.getElementsByTagNameNS("http://schemas.xmlsoap.org/soap/envelope/", "Body").item(0);
    if (soapBody !== undefined) {
      var gcResponse = soapBody.getElementsByTagNameNS("http://www.infosys.com/response/GetConsent","GetConsentResponse").item(0);
      if (gcResponse !== undefined) {
        var consentIDElement = gcResponse.getElementsByTagNameNS("http://www.infosys.com/response/GetConsent", "CONSENT_ID").item(0);
        if (consentIDElement !== undefined) {
          consentID = consentIDElement.textContent;
        }
        var consentExpiryElement = gcResponse.getElementsByTagNameNS("http://www.infosys.com/response/GetConsent", "CONSENT_EXPIRY").item(0);
        if (consentExpiryElement !== undefined) {
            consentExpiry = consentExpiryElement.textContent;
        }
      }
    }

    var metadata = {"CONSENT_ID": consentID, "CONSENT_EXPIRY": consentExpiry };
    var resp_string = JSON.stringify (metadata);
    context.message.statusCode = '200 Wonderful';

    apim.setvariable('message.headers.X-API-OAUTH-METADATA-FOR-ACCESSTOKEN', resp_string, 'set');

    apim.setvariable('message.headers.X-API-OAUTH-METADATA-FOR-PAYLOAD', resp_string, 'set');

     



    ------------------------------
    Ashok Beshra
    ------------------------------