req.body is empty in Node

こ雲淡風輕ζ 提交于 2021-01-28 22:08:51

问题


React (Client) sent post data through axios. but req.body is empty in Node server side. Tried to use body-parser. but failed. attached client side here

attached server code here

This is client Axios part


回答1:


It should be the Content-Type on the request.

Per default the body-parser "urlencoded" handles only the following:

Content-Type: application/x-www-form-urlencoded;

You can set the type so:

app.use(bodyParser.urlencoded({
  extended: true,
  type: 'multipart/form-data'
}))

But then you have to parse the "raw body" by yourself, because the body-parser doesn't support multipart.




回答2:


The body-parser doesn't support decoding multipart/form-data. There are ample of libraries available for parsing multipart-form/data.

I know the formidable library to be working and using it is as simple as this:

var form = new formidable.IncomingForm();

form.parse(req, function(err, fields, files) {

    console.log(`fields: ${fields} /n files: ${files}`)

});


来源:https://stackoverflow.com/questions/56748415/req-body-is-empty-in-node

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!