Global AI and Data Science

 View Only
  • 1.  JSON to XML

    Posted Fri January 06, 2023 04:54 PM
      |   view attached

    Hi,

    Can someone please help on how to create gateway script in IBM API Connect V10 that will be able to sign and XML document using a JSON payload?

    {
    "xmlString": "Test",
    "xmlID":"4"

    }

    Parse Policy: using detect
    Invoke Policy: using $(targetURL)$(request.search)
    Current gateway script example:

    var apim = require('apim');
    var targetURL = 'http://example.com' + apim.getvariable('api.operation.path');
    apim.setvariable('targetURL', targetURL);


    Thanks!



    ------------------------------
    Malfred Koryor
    ------------------------------


    #GlobalAIandDataScience
    #GlobalDataScience


  • 2.  RE: JSON to XML

    Posted Mon January 09, 2023 01:26 PM

    Hi Malfred!

    To create a gateway script in IBM API Connect V10 that will be able to sign an XML document using a JSON payload, you can use the following approach:

    1. Define a parse policy that will extract the XML string and XML ID from the JSON payload. You can use the detect policy to extract the data using the following code snippet:
    <detect>
         <variable name="xmlString" type="string"/>
         <variable name="xmlID" type="string"/>
    </detect>

    1. Create an invoke policy that will send the XML string and XML ID to the target URL specified in the gateway script. You can use the invoke policy with the $(targetURL)$(request.search) expression to send the data to the target URL, as shown in the following code snippet:

    <invoke url="$(targetURL)$(request.search)" verb="POST">
         <set-header name="Content-Type" value="application/xml"/>
         <set-body>
              <![CDATA[
              <xml>
                   <string>$(xmlString)</string>
                   <ID>$(xmlID)</ID>
              </xml>
              ]]>
         </set-body>
    </invoke>

    1. In the gateway script, you can set the targetURL variable to the desired URL and use the apim.setvariable() function to set the value of the targetURL variable. The updated gateway script will look like this:


    var apim = require('apim');
    var targetURL = 'http://example.com' +
    apim.getvariable('api.operation.path');
    apim.setvariable('targetURL', targetURL);

    This script will extract the XML string and XML ID from the JSON payload, send them to the target URL, and set the value of the targetURL variable for use in the invoke policy.

    I hope this helps. If you have any questions, say.



    ------------------------------
    Pedro Viegas
    ------------------------------