My problem is that req.body is returning ‘Object’. In the code below I get the TypeError message. The ‘req’ variable seems to contain nothing.
First, this is my curl testing command. I would like ‘here-again’ to show up in the file ‘/home/dave/test.txt’. I have tried putting ‘here-again’ in quotes.
curl -X PUT http://localhost:8008/config/ --data here-again
Below is my js code.
app.put('/config/', function (req, res) {
const filename = '/home/dave/test.txt';
const data = req.body;
//res.send('body ' + data + ' ' + filename );
console.log(data)
fs.writeFile(filename, data, (err) => {
if (err) {
console.error(err);
res.status(500).send('Error writing file');
} else {
res.send('Data saved successfully');
}
});
//
})
Below is my error message.
TypeError [ERR_INVALID_ARG_TYPE]: The “data” argument must be of type string or an instance of Buffer, TypedArray, or DataView. Received an instance of Object