1

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

asked Jun 25, 2019 at 6:57
4
  • 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. Commented Jun 25, 2019 at 6:59
  • Can you show us your axios request? Commented Jun 25, 2019 at 7:00
  • 1
    Looks 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. Commented Jun 25, 2019 at 7:23
  • Thank you guys. I will try.... FYI, Axios part is added. Would you please take a look again? Commented Jun 25, 2019 at 16:35

2 Answers 2

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.

answered Jun 25, 2019 at 7:15
Sign up to request clarification or add additional context in comments.

1 Comment

With "raw body" i mean the unparsed http body. For x-www-form-urlencoded its simple (key1=value1&key2=value2). For multipart its more complicated. Example: ec.haxx.se/http-multipart.html
0

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}`)
});
answered Jun 25, 2019 at 7:40

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.