Dan Hoang
Dan Hoang

Reputation: 23

post request not receiving a body

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.

Upvotes: 2

Views: 629

Answers (1)

JRichardsz
JRichardsz

Reputation: 16515

Add the header in your fetch config:

content-type: application/json

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

Upvotes: 3

Related Questions