Home > Net >  post request not receiving a body
post request not receiving a body

Time:11-21

I setup a post request from a js file like so:

fetch('/account/signup', {
    method: 'POST', 
    body: JSON.stringify({ username: document.getElementById('username').value, password: document.getElementById('password').value, email: document.getElementById('email').value, startingBal: document.getElementById('startingBal').value })
  }).then(response => response.json());
}

and I have a router receiving the post request

const router = express.Router()

router.use(bodyParser.urlencoded({ extended: true }))
router.use(bodyParser.json())

router.post('/signup', (req, res) => {
  console.log(req.body)
})

Yet it only logs {} to the console, so it's receiving the request but doesn't log anything.

CodePudding user response:

Add the header in your fetch config:

content-type: application/json

If it don't work, use postman and share the results

  • Related