Search code examples
node.jszipibm-cloudmime-typesserverless-framework

Send zip file as response in IBM Cloud Function


I have a serverless function deployed in IBM Cloud Functions. The function generates and downloads an excel file.

Locally it works as expected, but on the deployed version I get the following error:

{
  "code": "xxxxxxxxxx",
  "error": "Response type in header did not match generated content type."
}

With the Content-Type header 'application/json' I can send json and with 'image/png' even images as base64. Zip files are not working. I tried with 'Content-Type': 'application/zip and 'Content-Type': 'application/zip, application/octet-stream

Do I need to do some more configuration?

Full example:

headers: {
    'Content-Type': 'application/zip',
    'Content-Disposition': `attachment; filename=foo.zip;`,
    'Content-Transfer-Encoding': 'binary',
    'Content-Length': zip.length
},

Solution

  • I fixed it by generating a base64 zip instead of uint8array.

    Before:

    var JSZip = require("jszip");
    var zip = new JSZip();
    
    const myZip = await zip.generateAsync({
                    type: 'uint8array',
                    base64: true,
                    compression: 'DEFLATE'
                });
    

    After:

    var JSZip = require("jszip");
    var zip = new JSZip();
    
    const myZip = await zip.generateAsync({type: 'base64'});