I'm posting the data {id: "abc123", text: "sometext"} to a node API. Posting from a component with action call:
export function addTextToAPI(inputData) {
return(dispatch) => {
console.log(inputData),
dispatch(addText(inputData))
fetch(myapi, {
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
method: 'POST',
data: inputData
})
.then(res => res.json())
}
}
console.log(inputData) is {id: "abc123", text: "sometext"}
node:
var express = require('express');
var bodyParser = require('body-parser');
var app = express();
app.use(bodyParser.json());
app.post('/addtest', (req, res) => {
console.log(req.body); // <= returns blank object {}
console.log(req.body.data); // <= returns undefined
console.log(req.query); // <= returns blank object {}
console.log("test added");
});
app.listen(3000);
I want to be able to read the inputData in req. How do I do that? I want to be able to read the inputData in req. How do I do that?
JSON.stringify(inputdata)but it doesn't work.fetchin question. Please have a look.