How to response.send like the nodejs raw send

I’m trying to convert this express code ( to work in an adonisjs controller but running into an error “Cannot set headers after they are sent to the client” . Ultimately i’m trying to get http://pdfmake.org/ module to send a pdf to the user.

Also the docs say i can access the raw response object response.response. Can you provide an example? I tried to replace “res” with “response.response” in that express code sample below but it didn’t work.

express code

res.writeHead(200, 
        {
            'Content-Type': 'application/pdf',
            'Content-Disposition':'attachment;filename="filename.pdf"'
        });

        const download = Buffer.from(data.toString('utf-8'), 'base64');
        res.end(download);

adonis controller

response.header('Content-type', 'application/pdf');
const download = Buffer.from(data.toString('utf-8'), 'base64');
response.send(download);
1 Like

Hi. You can use the shorter

response.type('application/pdf')

But this wasn’t your problem. I think something has already written to the body of the response? If you remove the response.header(…) line does it download? If so. Can you open the download with a texteditor and see if something is written before %PDF which should be the first thing in the file.

1 Like