Reputation: 2804
fetch('/auth/signup', {
method: 'POST',
body: this.state.user,
headers: {
'Content-Type': 'application/json'
},
})
.then(function(response) {
return response.json();
})
.then(data => {
console.log(data);
})
this.state.user is basically an object like {user: {name:"someone"}
and my server side code simply do this
router.post('/signup', (req, res, next) => {
console.log(req.body) // empty object
}
I think something is wrong with fetch
, didn't see any data been pass in the network tab.
Upvotes: 5
Views: 12515
Reputation: 1
Pass this.state.user
to JSON.stringify()
before setting at body
or use FormData
to POST
key, value pairs. body
does not expect javascript
object.
body: JSON.stringify(this.state.user)
let fd = new FormData();
let {user} = this.state.user;
for (let prop in user) {
fd.append(prop, JSON.stringify(user[prop]));
}
body: fd
Upvotes: 8