How do you create XML payloads for the backend systems that works with XML?

I have some API proxies connecting with backend services that consume and produce XML. I as an API proxy developer have to convert JSON payloads to XML ones. Using JSON to XML policy does not provide expected results. So, I am using JS policy to create XML payloads like attaching multiple strings.

var xml = '';

xml += '<rootTag><header>\
' + someDynamicValue + '
</header></rootTag>';
context.setVariable('requestPayload', xml);

I don't like to create XML like this as they look messy and are sometimes hard to read.

Please advise me on how to handle this case. I am sure many people might have come across this scenario.

Thankyou

1 5 1,176
5 REPLIES 5

In what way does your JSON to XML not produce expected results?

Also, instead of using javascript, you could use an assign message policy for a static XML structure - ie assuming you dont have lists for example that vary between requests, or anything else where your structure will vary. There's an example here of using AssignMessage for XML

https://docs.apigee.com/api-platform/reference/policies/assign-message-policy#example-4_3

Thanks for your reply...

Well, this is one of the ways to produce XML. However, what it lacks is the manipulation of the XML payload. So for example, if you want to add a particular tag in the XML only if a dynamic value is available in the request. Otherwise, you want to skip adding that tag cos if you add it with an empty or null value that might result in a bad request from the backend service.

It sounds like you want to apply custom conditional logic as part of your transformation since you don't want empty XML tags. This is not supported by the JSON to XML policy..

However, you could apply an XSLT after generating the XML to further transform your XML structure.

You should probably performance test your solution if you have a larger pay load or higher traffic.

Alternatively, you could use javascript to remove empty/null json elements - This could be for specific elements, or by just going over all of your json object and removing any that are null from your json before running JSON to XML. This assumes your only concern is not including empty XML elements in the payload.

Not applicable

First of all, you can send the payload in request directly as XML.

If your payload needs to be modified, then modify the json payload and convert to XML.

You can use XSLT to transform the XML to particular format.

Here's how I do it. AssignMessage ! That's the main purpose of the AssignMessage policy.

<AssignMessage name='AM-1'>
  <DisplayName>AM-1</DisplayName>
  <AssignTo createNew='true' transport='http' type='request'>contrivedMessage</AssignTo>
  <IgnoreUnresolvedVariables>false</IgnoreUnresolvedVariables>
  <Set>
    <Payload contentType='text/xml'><soap:Envelope xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/' xmlns:ser='http://qx.example.com/services'>
      <soap:Header>
        <wsse:Security xmlns:wsse='http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd'
                       soap:actor='http://schemas.xmlsoap.org/soap/actor/next'>
          <wsse:UsernameToken>
            <wsse:Username>{my.username}</wsse:Username>
            <wsse:Password>{my.password}</wsse:Password>
          </wsse:UsernameToken>
        </wsse:Security>
        <ser:Profile>
          <ser:Name>Apigee TEST</ser:Name>
          <ser:Client>Apigee Edge Client</ser:Client>
          <ser:Adapter>SOAP API</ser:Adapter>
          <ser:Machine>MBPRO</ser:Machine>
        </ser:Profile>
      </soap:Header>
      <soap:Body>
        <ser:Ping/>
      </soap:Body>
    </soap:Envelope></Payload>
    <Verb>POST</Verb>
  </Set>
</AssignMessage>

As for dynamic values, the Payload element accepts a "message template", which means the policy treats strings within curly braces as variable references, and at runtime, replaces those references with the values of the variables. In the above example, you can see I'm using references like {my.username} and {my.password} to specify dynamic values into the text elements of the XML document.

More on message templates here.