minh quan nguyen
minh quan nguyen

Reputation: 61

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

Upvotes: 1

Views: 4820

Answers (4)

tyro
tyro

Reputation: 617

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

Upvotes: 0

Satya Dewangan
Satya Dewangan

Reputation: 21

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

Upvotes: 0

Roakz
Roakz

Reputation: 51

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}!`))

Upvotes: 2

Mohit Hurkat
Mohit Hurkat

Reputation: 416

Try using JSON Input for the Request.

enter image description here

Upvotes: 6

Related Questions