24

I am getting form data in this form

'------WebKitFormBoundarysw7YYuBGKjAewMhe\r\nContent-Disposition: form-data; name': '"a"\r\n\r\nb\r\n------WebKitFormBoundarysw7YYuBGKjAewMhe--\r\n

I'm trying to find a middleware that will allow me to access the form data like:

req.body.a // -> 'b'

I've tried

var express = require('express');
var app = express();


var bodyParser = require('body-parser');

// parse application/x-www-form-urlencoded 
app.use(bodyParser.urlencoded({ extended: false }))

Is there a problem with my implementation or am I not using the correct middleware?

3
  • 1
    I think body parser stopped supporting form data parsing. Their github page has alternative form data parsers like busboy. Commented Jun 4, 2015 at 21:50
  • I am trying busboy and my onfield event is giving me a fieldname of "------WebKitFormBoundary10WDBTqcGz382i2e ↵Content-Disposition: form-data; name" Commented Jun 4, 2015 at 21:59
  • bodyParser.urlencoded is used to parse URL encoded form data. You should look into a multipart/form-data parser. Commented Sep 7, 2022 at 3:12

6 Answers 6

17

The tool that worked was multiparty

app.post('/endpoint', function (req, res) {
    var form = new multiparty.Form();
    form.parse(req, function(err, fields, files) {
        // fields fields fields
    });
})
Sign up to request clarification or add additional context in comments.

1 Comment

but this tool is for multipart/data form i am just submiting text/plain form
12

The library which worked for me was express-formidable. Clean, fast and supports multipart requests too. Here is code from their docs

Install with:

npm install -S express-formidable

Here is sample usage:

const express = require('express');
const formidable = require('express-formidable');

var app = express();

app.use(formidable());

app.post('/upload', (req, res) => {
  req.fields; // contains non-file fields 
  req.files; // contains files 
});

1 Comment

It can also be used if you are using multer for any file upload/download logic. Such a great tool, thanks for sharing!
11

The above two answers are correct but now those methods are outdated. multer is a better method to access form data. Install it by the following command: npm install multer

some useful body parsers.

Body-type: parser

  • form-data: multer

  • x-www-form-urlencoded: express.urlencoded()

  • raw: express.raw()

  • json: express.json()

  • text: express.text()

1 Comment

According to expressjs doc, Multer will not process any form which is not multipart (multipart/form-data).
1

-better to use multer.
-But if you want to use bodyParser.urlencoded convert it into URLSearchParams data type

demonstration:-
 let fd=new FormData("id_of_form")
let sp=new URLSearchParams()
for(let [k,v]:fd.entries()) sp.append(k,v)
  • make header 'Content-Type':'application/x-www-form-urlencoded ' and pass sp to body.

-happy coding

Comments

1

You can use multer package:

const express = require('express');
const multer = require('multer');
const app = express();
const upload = multer();

app.post('/submit-form', upload.none(), (req, res) => {
  // Access form data here
  const formData = req.body;
  console.log(formData);
  
  // Handle the form data
  // ...
  
  res.send('Form submitted successfully');
});

The parsed form data is available in req.body.

Comments

0

if you wish to send the output of FormData, you may avoid the fuss and convert the form in the browser to json.

the solution that worked for me is the one in the link but there are more solutions that might interest you given to the same question.

Comments

Your Answer

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.