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
-
As I am a beginner, I couldn't post the code. Instead, I attached 2 images which shows code. Please help. I struggled with it for 2 weeks.Chan Song– Chan Song2019年06月25日 06:59:22 +00:00Commented Jun 25, 2019 at 6:59
-
Can you show us your axios request?Cat_Enthusiast– Cat_Enthusiast2019年06月25日 07:00:45 +00:00Commented Jun 25, 2019 at 7:00
-
1Looks like you are submitting the request with mutipart/form-data and your node is parsing for www-urlencoded. Change your request content-type in axios to www-urlencoded.ns15– ns152019年06月25日 07:23:20 +00:00Commented Jun 25, 2019 at 7:23
-
Thank you guys. I will try.... FYI, Axios part is added. Would you please take a look again?Chan Song– Chan Song2019年06月25日 16:35:38 +00:00Commented Jun 25, 2019 at 16:35
2 Answers 2
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.
1 Comment
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}`)
});