When a web service returns a multipart/related or MTOM response that you already hold in memory — for example, the body returned by a Web Service call made with the full response option, a SOAP MTOM attachment response, or a small DICOMweb response — qie.parseMultipartContent() splits it into an easy-to-read XML message. You get one <part> per multipart segment, and each part's bytes are base64-encoded inline so a script can read them directly.
This is the in-memory companion to qie.parseMultipartContentFromFile(). Use this one when the whole body comfortably fits in memory; use the FromFile variant when the body is large (e.g. a DICOMweb WADO-RS retrieve of many instances) and should be streamed to disk instead of buffered.
When to use it (vs. qie.parseMultipartContentFromFile)
| qie.parseMultipartContent | qie.parseMultipartContentFromFile |
|---|
| Input | The multipart body as an in-memory String or byte[] | A file path (String or File) to the streamed body |
| Where parts go | Base64-encoded inline in the XML (<content>) | Written to individual files on disk (<path>) |
| Memory use | Entire body buffered in memory | Bounded — parts are streamed to disk as they are read |
| Best for | SOAP MTOM responses and other small multipart bodies | Large WADO-RS retrieves and other big multipart/related bodies |
Rule of thumb: if the response could be large, stream it to disk and use qie.parseMultipartContentFromFile() instead.
Getting the multipart body
parseMultipartContent() works on content you already have in memory as a String or byte[]. Typically that is the body returned by a Web Service call made with the full response option, so the raw multipart/related / MTOM body (including the part boundaries) is handed back to your script rather than just the root document.
If you only need the SOAP root part with its xop:Include attachment references replaced inline (and don't care about the other parts on their own), use qie.parseSoapContent(content, true) instead. Use parseMultipartContent() when you want every part broken out individually.
Walkthrough
Parse the in-memory body, then iterate the parts and decode each one.
var content = null; // URL template for the endpoint var urlTemplate = qie.evaluateTemplate(qie.getWsOperationUrl("All DICOM Web APIs", "studies/{mc:study_uid}")); var parameterMap = qie.newParameterMap(); parameterMap.put("http.header.Accept", 'multipart/related; type="application/dicom"'); // Call a REST web service using the provided parameters. The 8th argument (true) returns the // full HTTP response envelope rather than just the body. var value = qie.callRESTWebService( "All DICOM Web APIs", urlTemplate, "GET", content, null, parameterMap, 60000, true); // 'value' is the <httpResponse> envelope. The multipart/related body lives in // /httpResponse/content, base64-encoded; parse the envelope and decode the body to bytes. var response = qie.parseXMLString(value); var bodyBytes = qie.base64DecodeToBytes(response.getNode('/httpResponse/content')); // Parse the body into an XML message: one <part> per segment, each part's bytes // base64-encoded inline in a <content> element. var partsXml = qie.parseMultipartContent(bodyBytes); // Iterate the parts and decode each one. var partCount = partsXml.getCount('/mtomContent/part'); for (var i = 1; i <= partCount; i++) { var partType = partsXml.getNode('/mtomContent/part[' + i + ']/partType'); var base64 = partsXml.getNode('/mtomContent/part[' + i + ']/content'); // DICOMweb parts are binary DICOM objects — decode to raw bytes. var bytes = qie.base64DecodeToBytes(base64); // (For textual parts, use qie.base64Decode(base64) to get a String instead.) // ... do something with partType + bytes ... }What the function returns
An XML message with one <part> per multipart segment. Each part's name attribute is its Content-ID, <partType> is its Content-Type, and <content> holds the part's bytes base64 encoded.
<mtomContent>
<part name="part1@example">
<partType>text/plain; charset=UTF-8</partType>
<content>VGhpcyBpcyB0aGUgZmlyc3QgcGFydA==</content>
</part>
<part name="part2@example">
<partType>application/octet-stream</partType>
<content>VGhpcyBpcyB0aGUgc2Vjb25kIHBhcnQ=</content>
</part>
</mtomContent>
Read it with the usual XML message functions, decoding the base64 as needed:
var partCount = partsXml.getCount('/mtomContent/part'); // how many parts
var firstType = partsXml.getNode('/mtomContent/part[1]/partType'); // first part's MIME type
var firstB64 = partsXml.getNode('/mtomContent/part[1]/content'); // first part, base64
var firstText = qie.base64Decode(firstB64); // ...decoded to text
byte[] input
The content argument can also be a byte[] (for instance, the raw bytes returned by a Web Service call). The result is identical:
var partsXml = qie.parseMultipartContent(multipartResponseBytes); // byte[] in, XML out
See Also: