Download a File from Response with Content-Type:multipart/mixed

I'm working with the Adobe PDF Services API and attempting to download the resulting PDF that is returned as multipart/mixed data. The raw response looks like this:

--Boundary_380665_2133110495_1627937751536
Content-Type: application/json
Content-Disposition: form-data; name="contentAnalyzerResponse"

{"cpf:inputs":{"documentIn":{"dc:format":"application/vnd.openxmlformats-officedocument.wordprocessingml.document","cpf:location":"InputFile0"},"params":{"cpf:inline":{"outputFormat":"pdf","jsonDataForMerge":{"customerName":"Kane Miller","customerVisits":100,"itemsBought":[{"name":"Sprays","quantity":50,"amount":100},{"name":"Chemicals","quantity":100,"amount":200}],"totalAmount":300,"previousBalance":50,"lastThreeBillings":[100,200,300]}}}},"cpf:engine":{"repo:assetId":"urn:aaid:cpf:Service-52d5db6097ed436ebb96f13a4c"},"cpf:status":{"completed":true,"type":"","status":200},"cpf:outputs":{"documentOut":{"cpf:location":"multipartLabel","dc:format":"application/pdf"}}}
--Boundary_380665_2133110495_1627937751536
Content-Type: application/octet-stream
Content-Disposition: form-data; name="multipartLabel"

%PDF-1.6
<< Snipped >>
startxref
116
%%EOF

--Boundary_380665_2133110495_1627937751536--

Note, the first part (application/json), contains the reference name of the octet-stream Content-Disposition in "cpf:outputs":{"documentOut":{"cpf:location":"multipartLabel" (in this case, "multipartLabel").

I also have access to the boundary ID in the response header.

How can I simply extract the PDF data from this response? I've attempted to use utils.downloadFile(my_query.data , "my_file", 'pdf'), but of course, the entire multipart document is not a proper PDF, so attempting to open it in Acrobat Reader fails. I'm afraid I have to resort to a complex javascript program to split, regex, match the data I care about.

Any help is appreciated! Thank you.

1 Like

Okay, so I got a little further, but not sure what to do with the data I have. The following script successfully separates the raw PDF data from the multipart response:

let contentType = pollStatus.metadata.headers['content-type'][0];
console.log(contentType);

let contentTypeParts = contentType.split(';');
let boundary = contentTypeParts[1].split('=')[1];
console.log("Boundary: ", boundary);

let parts = pollStatus.data.message.split(boundary).filter(part => part !== '--' && part !== '');

console.log("Parts: ", );

let parsedParts = parts[1].split('\n').filter(part => part !== '');
let shifted = parsedParts.splice(4, parsedParts.length - 2);

console.log("Shifted: ", shifted);
let pdfData = shifted.join();

However, when attempting to download it as a pdf using utils.downloadFile(pdfData, 'generated', 'pdf');, I get an error saying "Failed to Load PDF".

At this point, I'm attempting to remind myself that the content type is application/octet-stream. So, maybe I need to look into handling the data as a stream. That'll be my next step. Just hoping somebody has a good idea. :slight_smile:

1 Like

Having the same issue @victoria @alex-w

I've also posted this to the Adobe Community:

I attempted to go down the path of working with the stream using Blob as well as FileReader. I also attempted to use a function to convert the UTF8 text to base64, but no joy. By no means am I an expert in dealing with this octet-stream stuff, but there's gotta be an easier way or something I'm missing.