'POSTMAN can not read request body

i try to create post method via postman but postman can't read request body here is my code on index file

app.post('/users', async (req, res) => {
  const addUser = new User(req.body)
  console.log(req.body)
  try {
    await addUser.save()
    res.status(201).send(addUser)
  } catch (e) {
    res.status(400).send(e)
  }
})

this is capture when i use postman enter image description here and this is result of terminal http://prntscr.com/qvdvpj



Solution 1:[1]

It looks like your using express. For express to find raw data in a request you need to use a middleware. Look into app.use(express.json()).

You don't need to require it its a built in method. It would go in your server or app.js file. This would ensure that you can access json data in the request.

it used to be called body-parser which was a popular npm package but is now available through express.

In a basic server file it might look like this

const express = require('express')
const app = express()
const port = 3000

app.use(express.json())   <----- something like this 
app.use(myrouteshere)

app.listen(port, () => console.log(`Example app listening on port ${port}!`))

Solution 2:[2]

In my case, I used app.use(express.json()) and started working prefectly.

Solution 3:[3]

Sometimes there could be custom content-type header value instead of just application/json. Make sure you have the right one.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Satya Dewangan
Solution 3 tyro