I have a ColdFusion REST component. For one method (GET) I need to return a JSON response and a file to save.
I have this, but it's not doing what I hoped it would
// the getDocument function returns a PDF file
returnFile = getDocument(configuration, myNode);
getpagecontext().getresponse().setStatus( JavaCast( 'int', 200 ), "OK" );
cfcontent(type="application/json");
writeOutput(serializeJSON(outputPayload));
fName = "#payloadIn.documentName#.#lcase(payloadIn.documentMimeType)#";
cfheader(name="Content-Disposition", value="attachment;filename=#fName#");
writeOutput(returnFile);
I get the JSON and then the pdf dumps out after. I want to see the JSON and have the PDF be an attachment.
{
"documentKey": "0140481423",
"uniqueId": "18974-2025-000009",
"documentName": "TestDocument-0140481423",
"documentType": "Mitigating - Underwriting",
"documentMimeType": "PDF",
"statusCode": "0",
"statusMessage": "Document Retrieved Successfully"
}%PDF-1.5
%����
14 0 obj
<</Linearized 1/L 6775/O 16/E 1805/N 1/T 6472/H [
453 145
]>>
endobj
20 0 obj
<</DecodeParms<</Columns 4/Predictor 12>>/Filter/FlateDecode/ID[<5B0EF5B830411B4FADFBF95E8814CE7D><788615627186E146AEF8FEBF2FB7833E>
]/Index[
14 11
]/Info 13 0 R/Length 51/Prev 6473/Root 15 0 R/Size 25/Type/XRef/W[
1 2 1
]>>stream
h�bbd``b`rS��Y�DT
[and so on]
returnFile = getDocument(configuration, myNode);
fName = "#payloadIn.documentName#.#lcase(payloadIn.documentMimeType)#";
returnFile['fileData'] = ToBase64(FileReadBinary(fName));
getpagecontext().getresponse().setStatus( JavaCast( 'int', 200 ), "OK" );
cfcontent(type="application/json");
writeOutput(serializeJSON(outputPayload));
The response will have a key with the base64 representation if the file which client can read and write to a file.
JSON
{
"documentKey": "0140481423",
"uniqueId": "18974-2025-000009",
"documentName": "TestDocument-0140481423",
"documentType": "Mitigating - Underwriting",
"documentMimeType": "PDF",
"statusCode": "0",
"statusMessage": "Document Retrieved Successfully",
"fileData": "BASE64STRING"
}