I am trying to send a JSON array containing multiple files and input fields using axios. Processing the files and subsequent field and finally trying to send an object like :
[
{file : "",
text : ""
}, {...}, {...}
]
ServerA.js
const bodyParser = require('body-parser');
const multer = require('multer');
const upload = multer();
app.use(bodyParser.json());
app.use(upload.any());
app.post('/requestFile',(req,res) => {
let files = req.files;
let textInfo = req.body;
let form = [];
files.forEach((file,index)=>{
let formObj = {};
formObj.file = file;
let key = Object.keys(textInfo)[index];
let value = texInfo[key];
formObj.text = value;
form.push(formObj)
})
axios.request({
method: 'post',
url: `http://localhost:1001/receiveFiles`,
headers: {
'Content-Type': 'multipart/form-data'
},
data: {
form
}
}).then(response => {
res.send('No Err');
}).catch(err=>{
res.send('ERR')
})
})
But I'm getting an empty object when I try to print the body at the receiving end :
ServerB.js
app.use(bodyParser.json());
app.use(upload.any());
app.post('/receiveFiles', (req,res) => {
console.log("Print: " + JSON.stringify(req.body));
res.send('successful');
})
I'm getting the following result :
Print : {}
I'm new to Nodejs, it will be really great if someone can help me out