Cant get request payload in express js node
Your content-type header is text/plain. Please try replacing
app.use(bodyParser.json());
with
app.use(bodyParser.text());
You need to send: Content-Type: application/json
for bodyParser.json()
to work, without it, your JSON payload won't be parsed, that's why you get: {}
From the docs:
The bodyParser object exposes various factories to create middlewares. All middlewares will populate the req.body property with the parsed body when the Content-Type request header matches the type option, or an empty object ({}) if there was no body to parse, the Content-Type was not matched, or an error occurred.
Example using .fetch
:
fetch('http://localhost:4200/dashboard', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({dd: 'dd'})
});