isomorphic fetch post can't read body on server
Asked Answered
L

1

24

I'm struggling with this strange problem, I can't seem to solve. I'm using isomorphic fetch to to post data to a server. I'm sending the body as a JSON-string. But on the server, I can't read the body, it's just an empty object.

The stack is: node, react.

Here is the client-code:

handleSubmit = (event) => {
    const { dispatch } = this.props;

    fetch('/api/me', {
      method: 'POST',
      header: {
        'Accept': 'application/json',
        'Content-Type': 'application/json'
      },
      body: JSON.stringify({
        name: 'xxx'
      })
    })
    .then(response => response.json())
    .then( json => dispatch( login( json ) ))
    .catch( err => console.log(err) )
  }

The server code:

var jsonParser = bodyParser.json()
app.post( '/api/me', jsonParser, ( req, res ) => {
  console.log('req', req.body);
})

I've tried googling the problem. But the few solutions I found, didn't to the trick.

All help is much appreciated.

BR

Martin

// UPDATE //

figured it out, it was a silly 's', I had forgotten. 'header' should be 'headers'

Liam answered 5/12, 2015 at 10:14 Comment(0)
P
1

Thank you for the update that one of the params should be plural:

  headers: {
    'Accept': ...,
    'Content-Type': ...
  },

You appended it to the question. Please feel free to accept this as the answer, or to create a new answer with that text, and accept it. Then the "unanswered" queue will contain one less dangling entry for folks to stumble upon.

Paviour answered 29/12, 2017 at 2:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.