Original Message:
Sent: Mon November 04, 2024 02:53 PM
From: Theresa Furlong
Subject: PDF stream to multi-part form data in gateway script
Steve,
Thank you so much. That makes complete sense, and the call does work now. I really appreciate the assistance.
------------------------------
Theresa Furlong
Original Message:
Sent: Mon November 04, 2024 02:36 PM
From: Steve Linn
Subject: PDF stream to multi-part form data in gateway script
Hi Teresa,
A couple of points.
First, trace data. I'm not sure how you're getting this data, but my assumption about the base64 decoded data is just the way the trace is presenting the data. A .pdf is going to contain binary data which is not going to create a well formed JSON property in the trace object, therefore it must be getting converted to base64 for that reason.
Second, I took your code and pasted it into a new API and posted a .pdf file to it. What I received as a response was missing the ending boundary and also a lot of my pdf file. Some of the .pdf file that I did receive appeared to have translated/garbled characters as well which supported my theory that your issue was due to the transformation of your buffer to a string which by default does a utf-8 encoding. Some of those characters you can see get garbled, and I'm guess that if within a string a hex 00 is encountered, that's taken as an end of string and your response is being truncated. The reason your buffer gets an implicit toString() function is that your code is doing string concatenation using your buffer, which will toString() your buffer.
I've posted this in other multipart threads in this forum, but your answer is going to be to deal with buffers only to prevent any implicit toString() functions. That would mean doing your string concatenations where they make sense, but creating a buffer from that string and using your buffer with your pdf data asis. If you place each buffer in an array, you can do a Buffer.concat to produce one final buffer from the multiple parts. My testing this appears to work but please test and verify. Also note that I did remove your final cflf after your final boundary as I didn't think that was necessary.
context.message.body.readAsBuffer(function (error, buffer) { if (error) { context.reject('InvalidData', 'Invalid Data format'); } var boundary = "--------------------------123456789123456789123456"; //Line break var crlf = '\r\n'; //Create the multipart message payload using a buffer array var bufferArray = []; bufferArray.push(Buffer.from('--' + boundary + crlf + 'Content-Type: application/pdf' + crlf + 'Content-Disposition: form-data; name="test"; fileName = "test.pdf"' + crlf + crlf)); bufferArray.push(buffer); bufferArray.push(Buffer.from(crlf + '--' + boundary + '--')); var contentType = 'multipart/form-data; boundary=' + '--------------------------123456789123456789123456'; context.message.body.write(Buffer.concat(bufferArray)); context.message.header.set('Content-Type', contentType);});
Best Regards,
Steve Linn
------------------------------
Steve Linn
Senior Consulting I/T Specialist
IBM
Original Message:
Sent: Mon November 04, 2024 08:12 AM
From: Theresa Furlong
Subject: PDF stream to multi-part form data in gateway script
Thanks Steve,
I'm still getting {"Message":"Unexpected end of MIME multipart stream. MIME multipart message is not complete."}.
This is my current code:
context.message.body.readAsBuffer(function (error, buffer) {
if (error) {
context.reject('InvalidData', 'Invalid Data format');
}
var boundary = "--------------------------123456789123456789123456";
//Line break
var crlf = '\r\n';
//Create the multipart message payload
var multipart = '--' + boundary + crlf +
'Content-Type: application/pdf' + crlf +
'Content-Disposition: form-data; name="test"; fileName = "test.pdf" "' + crlf + crlf +
buffer + crlf + '--' + boundary + '--' + crlf;
var contentType = 'multipart/form-data; boundary=' + '--------------------------123456789123456789123456';
context.message.body.write(multipart);
context.message.header.set('Content-Type', contentType);
});
When I look at the trace data, the response from the initial invoke from the pdf retrieval service looks like this(I did not include the entire response):
"JVBERi0xLjINJcXZ1cnF/w0xIDAgb2JqDTw8DS9BdXRob3IgKFBybyBBUEkpDS9DcmVhdGlvbkRhdGUgKEQ6MjAyNDExMDQwNzA3MDlaKQ0vTW9kRGF0ZSAoRDoyMDI0MTEwNDA3MDcwOVopDS9DcmVhdG9yIChQUk8gSExDQVBJKQ0vVGl0bGUgKFBSTyBEb2N1bWVudCkNL1Byb2R1Y2VyIChDcmF3Zm9yZFRlY2ggUERGIERyaXZlciBWZXJzaW9uIDQuMTIgNjQgQml0IEJ1aWxkIElEIDcwMTcgb24gQXByaWwgMDUsIDIwMjEgYXQgMjA6MDA6MzMpDS9TdWJqZWN0ICgpDS9LZXl3b3JkcyAoKQ0+Pg1lbmRvYmoNNSAwIG9iag08PA0vTGVuZ3RoDTcyMA0vRmlsdGVyDS9GbGF0ZURlY29kZQ0+Pg1zdHJlYW0NCnic3ZjRbtsgFIafoO9wLiZtkxoCGIzdO2LTxK0Drk3atV21i1Xb1a4n7elH0iRLuiSQRq3r/FIEwTbm4/hwDmCEMYafgGflcF7a764iKIbfgOEC7h9c8Ti/FnbvwJ5gV7GP0M+bpiAlcX8RZWB/AMHTJ8H+cTcQ6BFXxITOOnOtv0Ag6qo8xYjEsy7g/hO8e31+AHtx0pe6KmkZ7wvLIhStwcpBlqvztqG2aQ77UtMKihhuxbRyrOoikxq0tIXRsoRCN5Na6kxBZsaV1LfbaHcSxRHitBUioxWMjclvoSrlnQx7KICIMxQzD1E2qovGOf2lUudqjzFXmT5z3wrplURw9vzqUJbXqrFGn4L9AkJwjjd1sv58ABHjSPgWFBFhAmPVXE0Kq+DGzSvkNeibtRf3cHp518Oroyq0zGxxraDaYxp2K4AoilGSeIhkOZhcTVTtfqegx5AIQjfO5zM1lcoK5x8jqfOy0MOzNyGiyVNQ6dSiv5OIpIi0szK8SAFEmKzEqm/vXQFELHXxaOlHX3EUtW0Fj2ZDDOBKIhTjbnL165wkskz2TC2YYCg9...
After the gateway script executes this is the trace data:
entire message.body:
body: "----------------------------123456789123456789123456 Content-Type: application/pdf Content-Disposition: form-data; name="test"; fileName = "test.pdf" " %PDF-1.2 %������ 1 0 obj << /Author (Pro API) /CreationDate (D:20241104070709Z) /ModDate (D:20241104070709Z) /Creator (PRO HLCAPI) /Title (PRO Document) /Producer (CrawfordTech PDF Driver Version 4.12 64 Bit Build ID 7017 on April 05, 2021 at 20:00:33) /Subject () /Keywords () >> endobj 5 0 obj << /Length 720 /Filter /FlateDecode >> stream x�ݘ�n� ����p.&m���;b�ĭ�Mڵ]��U�ծ'��G�$K�$�F���R�6���p`�1���g�p^��"(�߀��\�8�v���`W���ϛ� %qe`"
It appears that it is base64 decoded data and not all of the message. I'm using buffer so not sure why it is decoding it and not including the entire message body that I created.
Any thoughts?
------------------------------
Theresa Furlong
Original Message:
Sent: Fri November 01, 2024 02:27 PM
From: Steve Linn
Subject: PDF stream to multi-part form data in gateway script
Hi Theresa,
For sure not providing the payloads with the multipart boundaries will result in MIME multipart message is not complete
if the Content-Type header indicates a multipart/form-data
and you simply pass in your buffer which would simply be your pdf file. As for the error File validation failed: File type is unknown
, I don't believe application/binary
is a valid Content-Type MIME value. Have you tried application/pdf
for the Content-Type header of your pdf file?
Best Regards,
Steve Linn
------------------------------
Steve Linn
Senior Consulting I/T Specialist
IBM
Original Message:
Sent: Fri November 01, 2024 07:37 AM
From: Theresa Furlong
Subject: PDF stream to multi-part form data in gateway script
I need to take a pdf byte stream returned from a call to backend service and pass it to a service expecting multipart form data. I've attempted many iterations but have not been successful. Currently I am using this code:
context.message.body.readAsBuffer(function (error, buffer) {
if (error) {
context.reject('InvalidData', 'Invalid Data format');
}
var bufferbase64 = buffer.toString('base64');
var bufferbin = Buffer.from(bufferbase64, 'binary');
var boundary = "--------------------------123456789123456789123456";
//Line break
var crlf = '\r\n';
//Create the multipart message payload
var multipart = '--' + boundary + crlf +
'Content-Type: application/binary' + crlf +
'Content-Disposition: form-data; name="test"; fileName = "test.pdf" "' + crlf + crlf +
bufferbin + crlf + '--' + boundary + '--' + crlf;
var contentType = 'multipart/form-data; boundary=' + '--------------------------123456789123456789123456';
context.message.body.write(multipart);
context.message.header.set('Content-Type', contentType);
});
The backend service is returning
{Message: "File validation failed: File type is unknown."}If I try passing the buffer, as is, the backend service returns
{"Message":"Unexpected end of MIME multipart stream. MIME multipart message is not complete."}
Can anyone assist?
Thank you.
------------------------------
Theresa Furlong
------------------------------