Document Upload Proxy

Not applicable

Hi All,

I've got a document upload proxy that's been running for a few months. It works well with text based files but when it is used for binary files, it corrupts them during the upload.

The Proxy:

The proxy accepts a multipart/form-data POST request with the file to be uploaded. The proxy then creates a new multipart request with added form fields containing metadata about the file, this is what is submitted to the backend service. This logic is accomplished in a JS policy that builds up the multipart request as a string.

var fileContent = context.getVariable("request.content")
var boundary = extractBoundary(fileContent)

var multipart = "--" + boundary+"\r\n"
           + "Content-Disposition: form-data; name=\"Document Information.FIELD1\""
           + "\r\n\r\n" + field1 + "\r\n"
           + "--" + boundary+"\r\n"
           + "Content-Disposition: form-data; name=\"Document Information.FIELD2\""
           + "\r\n\r\n" + field2 + "\r\n"
           + "--" + boundary+"\r\n"
           + "Content-Disposition: form-data; name=\"Document Information.FIELD3\""
           + "\r\n\r\n" + field3 + "\r\n"
           + fileContent //already has boundaries set so no need for boundaries.Also this must be the last item because of boundary formatting

The Problem:

The problem seems to be a corruption of the file content, which also probably results in the content-length being passed incorrectly to the back end. When I compare the original and downloaded file in a text editor, the content looks different

6264-capture.png

6265-compare.png

I'm not entirely sure how to resolve this - I'm guessing that it has something to do with my handling of the request content as a string and appending the additional multipart fields to it, but what other choice do I have for building this message up?

Solved Solved
0 2 838
1 ACCEPTED SOLUTION

You might be able to do it in Python callout. I don't speak python, so, not sure.

For sure you'd be able to do it in a Java callout.

View solution in original post

2 REPLIES 2

You might be able to do it in Python callout. I don't speak python, so, not sure.

For sure you'd be able to do it in a Java callout.

I went ahead and rewrote the code to use a Java callout. It works well 🙂

I think the difference is that in java, I can read the binary file as a stream i.e. messageContext.getMessage().getContentAsStream() which is where I suspect the corruption happened in the javascript policy's context.getVariable() method - which probably converts the binary file to a string.