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.


#API Connect
#Applicationintegration
#APIConnect
Β 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
    ------------------------------



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

    Posted Thu November 06, 2025 05:51 AM

    Hi Stieve,

    We are currently facing a challenge in exposing a SOAP service through IBM API Connect. The WSDL and its associated XSDs have been successfully deployed to the API Manager. However, when attempting to access the service via the browser using the ?wsdl query parameter, the returned WSDL reference points to a generic endpoint without a unique identifier URI.

    πŸ” Observed Behavior:

    • Accessing the WSDL via the browser (e.g., https://apic-1-gw-gateway-cp4i-dev.apps.cloudpak-ocp-nonprod.XXXXX.com/XXX-dev/$catalog_name/?wsdl) returns a fixed reference to one specific WSDL.

    • The returned WSDL contains a generic service endpoint without a unique path or identifier, making it difficult to distinguish between services.

    • Attempts to manually update the service tag in the WSDL with a specific path were unsuccessful-the reference still resolves to the generic endpoint.

    • Suspected caching issue: republishing the APIs under different products did not resolve the behavior.

    • This issue affects approximately 40 SOAP APIs.

    ❓ Request for Guidance:

    • Is there a recommended approach to ensure each SOAP service exposes a unique URI in the WSDL reference?

    • Can this issue be tested and resolved by updating one or two APIs first, or is it necessary to update all 40 APIs to observe the effect?

    • Are there any known caching mechanisms or configuration steps in API Connect that could be influencing this behavior?

    πŸ“Œ Additional Notes:

    We are looking for a scalable and maintainable solution that allows proper exposure of each SOAP service with a distinct endpoint. Any best practices or configuration tips would be greatly appreciated.



    ------------------------------
    Basma Hesham
    ------------------------------



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

    Posted Sat November 08, 2025 01:11 PM

    Hi Basma,

    As I don't have an environment anymore to play around to test out your use case, I'd suggest you open a PMR on this one.

    Best of luck!
    Steve



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



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

    Posted Tue November 11, 2025 05:24 AM

    Hi Steve,

    Thanks for your support.

    Would you mind to have a quick call to try this ? it'd be very appreciated.



    ------------------------------
    Basma Hesham
    ------------------------------



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

    Posted Wed November 12, 2025 06:17 PM

    Hi Basma,
    You should really open a PMR to get the support you need.  I've been retired from IBM now for almost a year (hard to believe how fast the time has gone by :-) ) so my knowledge is getting dated.  My experience of queries with ?wsdl were that I thought you should get the wsd l that you used to create the API, but I never had the occasion to look into the details of what was actually returned and I would have no ability now to even try this as I have no environment.  IBM support would have this and should be able to answer your questions.

    Best regards,

    Steve



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