Sidebar

How do I parse an MTOM Response from a rest web service call?

0 votes
1.3K views
asked Feb 6, 2019 by ben-s-7515 (12,850 points)

I am making a simple REST web service call, and the response content is MTOM formatted with multiple parts in it.  Is there an easy way to parse this and process each part individually?

 

Here is an example of the message received back:

---------------7afb50349c2148c3a5d6a324891a481c
Content-Type: application/xml

<contentData></contentData>
---------------7afb50349c2148c3a5d6a324891a481c
Content-Type: application/json

{"contentData":"nothing"}
---------------7afb50349c2148c3a5d6a324891a481c---------------

1 Answer

0 votes

Yes, from your script you will call the code wizard function "qie.parseMtomContent()".  This will parse the content for you and return an easy to work with XML structure with the data.

To parse the MTOM content for the sample message above and place it as your new XML message you would execute the following script:

// parse mtom content, and place in new XML message
var mtomContent = qie.parseMtomContent(source.getNode('/'));
message = qie.createXMLMessage(mtomContent);
 

The message will now be an XML message that looks like this:

<mtomContent>
   <part name="QveraGeneratedRootContentId_0">
      <partType>Content-Type: application/json</partType>
      <content-type>application/json</content-type>
      <content>eyJjb250ZW50RGF0YSI6Im5vdGhpbmcifQ0=</content>
   </part>
   <part name="QveraGeneratedRootContentId">
      <partType>Content-Type: application/xml</partType>
      <content-type>application/xml</content-type>
      <content>PGNvbnRlbnREYXRhPjwvY29udGVudERhdGE$DQ==</content>
   </part>
</mtomContent>

To cycle each of the parts, you will run a for loop on the different parts like this:

// cycle content parts
var parts = message.getAllNodes('/mtomContent/part');
for (var currentPart = 0; currentPart < parts.length; currentPart++) {
   var part = qie.parseXMLString(parts[currentPart]);
   qie.debug('Part: ' + part.toString());
   
   var content = qie.base64Decode(part.getNode('/part/content'));
   qie.debug('Content: ' + content);
}

This will log out each part individually, and then log out the content for that part.

 

See also:

Handling HTTP requests and responses

Making Requests via qie.callRESTWebService()

answered Feb 6, 2019 by ben-s-7515 (12,850 points)
edited May 16 by rich-c-2789
...