Sidebar

How do a make a multipart/form-data post to a webservice endpoint

0 votes
681 views
asked Mar 1, 2023 by ben-s-7515 (12,640 points)
I have a remote service that wants to receive data as a multipart/form-data content-type.  How do I properly make this request from QIE?

1 Answer

0 votes

When considering the boundary definition, please note the following:

1) In Postman the boundary definition in the content-type header starts with 26 dashes and then has 24 numbers, this actual value shouldn't matter, but want to note it.

2) Whenever the boundary is referenced, It is preceeded by an extra two dashes, so in Postman there are 28 dashes total.

With this in mind, we will do the following for our script:

// Set up the boundary to be 26 dashes, and 24 digits
var boundary = '--------------------------' + qie.getUUID(true).substring(0,24);

// Read binary data up if you don't already have it
var fileBytes = qie.readFile('{path-to-binary-file}');
var otherPartBytes = new java.lang.String("Other Part Data").getBytes();

// Set up the parameter map (Used to override header values)
var parameterMap = qie.newParameterMap();

// If sending binary data, encoding must be set to
// ISO-8559-1;  however, this is only so QIE can properly decode the string, so we also
// need to override the content-type so that the encoding is not also sent with the request
parameterMap.put('http.header.encoding', 'ISO-8859-1');
parameterMap.put('http.header.Content-Type', 'multipart/form-data; boundary=' + boundary);

var baos = new java.io.ByteArrayOutputStream();
// Create your multipart content. Each item in the multipart must
// be surrounded by the boundary
// NOTE: whenever we reference the boundary, we need to add two dashes to the start
//               and then end with '\r\n'
// NOTE2: We end the part headers with a blank line

var part = new java.lang.String(
   '--' + boundary + '\r\n' +
   'Content-Disposition: form-data; name="file"; filename="{file-name}.pdf"\r\n' +
   'Content-Type: application/pdf\r\n' +
   '\r\n').getBytes();

// write our part to our buffer for the content
baos.write(part, 0, part.length);
baos.write(fileBytes, 0, fileBytes.length);

// We also add other parts, note that we need to end the previous part with a '\r\n', and
//     then add our boundary preceeded by two dashes and ending with another '\r\n'
// NOTE: We end the part headers with a blank line

var otherPart = new java.lang.String(
   '\r\n' +
   '--' + boundary + '\r\n' +
   'Content-Disposition: form-data; name="otherData"; filename="otherData"\r\n' +
   'Content-Type: {based-on-the-data-you-are-sending}\r\n'  +
   '\r\n').getBytes();

// write our part to our buffer for the content
baos.write(otherPart , 0, otherPart .length);
baos.write(otherPartBytes , 0, otherPartBytes .length);

// now we end with a line ending ('\r\n'), then two dashes, then our boundary,
//    then a final two dashes, then a final line ending ('\r\n')
var endingBoundary = new java.lang.String(
   '\r\n--' + boundary + '--\r\n').getBytes();

// write our ending to our buffer for the content
baos.write(endingBoundary, 0, endingBoundary.length);

// convert the bytes into a string, but we are making sure that java knows to convert them
//   using 'ISO-8859-1' to account for binary data
var content = new java.lang.String(baos.toByteArray(), 'ISO-8859-1');

// call the ws-connection that is ready to receive this request
var urlTemplate = qie.evaluateTemplate(qie.getWsEndpointUrl("{ws-connection}"));
var value = qie.callRESTWebService(
   "{ws-connection}",
   urlTemplate,
   "POST",
   content,
   'multipart/form-data; boundary=' + boundary,
   parameterMap,
   60000);

answered Mar 1, 2023 by ben-s-7515 (12,640 points)
...