'Content-Disposition Header name must be a valid HTTP token on Node
So in my server, I'm trying to set an header, which in this case is this:
res.setHeader({
'Content-Disposition': 'attachment; filename=' + req.params.filename + '.pdf',
});
I'm using this so when I download the file I get the name on the client, but is not working for now, I get this weird error message:
TypeError [ERR_INVALID_HTTP_TOKEN]: Header name must be a valid HTTP token ["{
'Content-Disposition': 'attachment; filename=0.7105216890033654.pdf'
}"]
Here is extra understanding, I'm trying to download the file that is being send from server.
Here is the full handling on server:
downloadInvoice: (req, res) => {
res.setHeader({
'Content-Disposition': 'attachment; filename=' + req.params.filename + '.pdf',
});
res.download(`${path}/${req.params.filename}.pdf`,
(err) => {
fs.unlinkSync(`${path}/${req.params.filename}.pdf`);
}
);
And here is how I'm handling it on the front:
fetch(`${api}/orders/download/${res.payload.filename}`, {
responseType: 'blob',
'Content-Type': 'application/pdf',
})
.then((res) => downloadFile(res))
.catch((err) => console.log(err));
const downloadFile = async (res) => {
const url = window.URL.createObjectURL(await res.blob());
const link = document.createElement('a');
link.href = url;
link.setAttribute('download', `${filename}.pdf`);
document.body.appendChild(link);
link.click();
};
Solution 1:[1]
setHeader function takes two arguments, not an object.
try this :
res.setHeader('Content-Disposition', 'attachment; filename=' + req.params.filename + '.pdf');
as you are passing an object whole object is converted to string as header name for first argument and empty value for second argument.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | timbersaw |
