Sidebar

How can I send a binary file in a webservice call?

0 votes
851 views
asked Nov 10, 2017 by mike-r-7535 (13,830 points)
retagged Nov 2, 2018 by mike-r-7535
I have a zip file that I need to send over a webservice call, but the zip is unreadable on the other side.

2 Answers

0 votes

With binary data, like zip and PDF files, the encoding must always be 'ISO-8859-1'.  If that data is loaded as a 'UTF-8' String, the data will be mangled, and will not work.

Let's start with the payload.  If you have the raw bytes of the zip file loaded into a BinaryMessage, you will need to represent those bytes as a String to send it in a webservice call. You can preserve the raw encoding with the following:

var rawString = new java.lang.String(message.getBytes(), 'ISO-8859-1');

Also, binary data in a webservice call requires MTOM.  You can specify the encoding to use in the MTOM by adding the following to your parameterMap:

parameterMap.put('http.header.encoding', 'ISO-8859-1');

This will preserve the raw 'ISO-8859-1' encoding from the message to the payload, and from the payload to the actual webservice call.

answered Nov 10, 2017 by mike-r-7535 (13,830 points)
0 votes

As an example of this, to implement a DICOM STOW RS, you will need to send the binary DICOM format.  Here is an example of a mapping function to do so:

var messageBoundary = "DICOM-" + qie.getUUID(false);
var parameterMap = qie.newParameterMap();

// The ISO-8859-1 encoding will not mangle the bytes
parameterMap.put("http.header.encoding", "ISO-8859-1");
var urlTemplate = qie.evaluateTemplate("http://localhost:1234/");
var multipartContent = "--" + messageBoundary + "\r\n" +
   "Content-Type: application/dicom\r\n" +
   "\r\n" +
   message.getNode("/") +
   "\r\n" +
   "--" + messageBoundary + "--\r\n";

var value = qie.callRESTWebService(
   "dicomWS",
   urlTemplate,
   "POST",
   multipartContent,
   'multipart/related; type="application/dicom"; boundary=' + messageBoundary,
   parameterMap,
   60000);
messageCache.setValue("response", value);

Also note that the content must be sent as MTOM multipart message.

answered Nov 2, 2018 by mike-r-7535 (13,830 points)
...