I'm using Kuzzle as a backend for all my business logic for a mobile application and multiple websites. On one website, I need to download a PDF containing multiple reports. To stay in the same architecture, I want to achieve this in a custom Kuzzle plugin.
The default response of a plugin is wrapped in a default HTML response , how ca I precise custom headers to send a file ?
You can use the Request.setResult method to specify that your a returning a raw payload and to modify response headers.
Then simply return a Buffer
containing the file content.
async sendPdf (request) {
const file = fs.readFileSync('./file.pdf');
request.setResult(null, {
// Tell Kuzzle that this result will contain a raw payload
raw: true,
headers: {
// Set HTTP response headers
'Content-Length': file.length.toString(),
'Content-Type': 'application/pdf',
'Content-Disposition': `attachment; filename="file.pdf"`,
'Cache-Control': 'no-cache'
}
);
return file;
}
Of course this will only work with the HTTP or WebSocket protocol.