19

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?

Dan Baker
  • 1,757
  • 3
  • 21
  • 36
  • 1
    I think body parser stopped supporting form data parsing. Their github page has alternative form data parsers like busboy. – bspates Jun 04 '15 at 21:50
  • I am trying busboy and my onfield event is giving me a fieldname of "------WebKitFormBoundary10WDBTqcGz382i2e ↵Content-Disposition: form-data; name" – Dan Baker Jun 04 '15 at 21:59
  • `bodyParser.urlencoded` is used to parse URL encoded form data. You should look into a `multipart/form-data` parser. – OGreeni Sep 07 '22 at 03:12

5 Answers5

15

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
    });
})
Dan Baker
  • 1,757
  • 3
  • 21
  • 36
10

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 
});
Chandan Purohit
  • 2,283
  • 1
  • 17
  • 16
8

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()

Dhruvin modi
  • 613
  • 1
  • 8
  • 13
0

-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

0

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.

HamzaSaeed
  • 11
  • 4