1

I am trying to read formData that I send to my server. First I append something to a formData object. It looks like this:

const formData = new FormData();
formData.append('email', this.email); 

My post request looks like this:

axios.post('/api/auth/register', formData, {
  headers: {
    'accept': 'application/json',
    'Accept-Language': 'en-US,en;q=0.8',
    'Content-Type': `multipart/form-data; boundary=${formData._boundary}`,
  }
})

My backend route:

router.post('/register', (req, res) => {
  //this does log the email 'key', but the value is empty
  console.log('email: ', req.body)
  res.status(200).end()
})

How can I 'access' the email value on the server?

I've tried parsing it first with

email = JSON.parse(req.body.email);

But whatever I do, I cannot seem to get the value that I send...

I do have bodyParser installed. This is in my app.js

app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: true }))
Reinier68
  • 2,450
  • 1
  • 23
  • 47
  • For Express, you need the package "body-parser". Did you install it? – Romain V... Oct 21 '20 at 15:42
  • Yes, this is in my app.js `app.use(bodyParser.json()) app.use(bodyParser.urlencoded({ extended: true }))` – Reinier68 Oct 21 '20 at 15:42
  • Does this answer your question? [How to process POST data in Node.js?](https://stackoverflow.com/questions/4295782/how-to-process-post-data-in-node-js) – BadPiggie Oct 21 '20 at 15:43
  • Unfortunately not. It has to be formData because I'm also sending a file with the request. – Reinier68 Oct 21 '20 at 15:46
  • 1
    [multer](https://github.com/expressjs/multer) is like the de-facto solution for handling multipart/form-data. You can take a look at [formidable](https://github.com/node-formidable/formidable) also – Jordan Quagliatini Oct 21 '20 at 16:05

0 Answers0