I am sending a multipart data from a nodeJS route as a response.
I used form-data
library to accomplish, my requirement is to send additional header information for all binary data.
I tried the following
router.get('/getdata', async (req, res, next) => {
var form = new FormData();
var encodedImage2 = fs.readFileSync('./public/image2.png');
var CRLF = '\r\n';
var options = {
header: CRLF + '--' + form.getBoundary() + CRLF + 'X-Custom-Header: 123' + CRLF + CRLF
};
form.append('image2.png', encodedImage2, options);
res.set('Content-Type', 'multipart/form-data; boundary=' + form.getBoundary());
form.pipe(res);
});
in the output I get only the header
X-Custom-Header: 123
without the option
object I can get
Content-Disposition: form-data; name="image2.png"
Content-Type: application/octet-stream
I need an output headers something like
Content-Disposition: form-data; name="image2.png"
Content-Type: application/octet-stream
X-custom-Header: 123
I found the solution myself, and its very simple. Its posible to set custom headers in the options object header property
router.get('/getdata', async (req, res, next) => {
var form = new FormData();
var encodedImage2 = fs.readFileSync('./public/image2.png');
var options = {
header: {
'X-Custom-Header': 123
}
};
form.append('image2.png', encodedImage2, options);
res.set('Content-Type', 'multipart/form-data; boundary=' + form.getBoundary());
form.pipe(res);
});