6

I have a simple form:

<form method="post" action="/test">
  <input type="hidden" name="arr[]" value="val1">
  <input type="hidden" name="arr[]" value="val2">
  <input type="hidden" name="arr[]" value="val3">
  <input type="submit" value="Submit">
</form>

With the controller:

//...
server.post('/test', function(req, res) {
    res.json(req.body);
});
//...

This returns fine with:

{
  arr: [
    "val1",
    "val2",
    "val3"
  ]
}

However, when I change the enctype to multipart/formdata

<form method="post" action="/test" enctype="multipart/form-data">
  <input type="hidden" name="arr[]" value="val1">
  <input type="hidden" name="arr[]" value="val2">
  <input type="hidden" name="arr[]" value="val3">
  <input type="submit" value="Submit">
</form>

The server now responds with:

{
  arr[]: "val3"
}

What's the issue? Is there some kind of configuration I need?

In case you're wondering, I'm also sending a file, that why I need the multipart/form-data.

Kpaekn
  • 339
  • 3
  • 9
  • 1
    Looks more like multiparty does not handle this situation correctly. Would be interested to see what comes out. If you find no solution, try to report it under: https://github.com/andrewrk/node-multiparty/issues – Oleg Sklyar May 15 '14 at 09:42
  • Did you find out what was wrong? I have the same problem here – Oriesok Vlassky Apr 26 '16 at 15:28
  • The array syntax is a non-standard extension to form syntax. Your body parse for url encoded data recognises it but your body parser for multipart data does not. You didn't provide a [mcve] so we don't know what body parsers you are using so we can't tell if it is because there is no support or if support just needs to be enabled. – Quentin Aug 30 '17 at 12:51

1 Answers1

0

It's probably related to the use of body-parser (especially the urlencoded method) which, by default, works on requests with the media-type application/x-www-form-urlencoded only.

Your main application module probably has some lines like these:

const bodyParser = require('body-parser');
app.use(bodyParser.urlencoded());

I suppose, you could just add the following to have requests of type multipart/form-data parsed as well:

app.use(bodyParser.urlencoded({
  type: 'multipart/form-data'
}));
Tobi Kremer
  • 618
  • 6
  • 22
  • The multipart syntax is completely different to the urlencoded syntax. You can't use a parser for one with the other. – Quentin Aug 30 '17 at 12:50
  • Whoops, my bad! But body-parser has no multipart syntax, you'll need a dedicated module for handling multipart requests, like busboy or multer, as mentioned in the body-parser documentation. – Tobi Kremer Aug 30 '17 at 13:04
  • They're already using one (otherwise they wouldn't get the results they said they were getting), but we don't know which. – Quentin Aug 30 '17 at 13:05
  • Just a wild guess: The question is tagged with `kraken.js`, so it might be [formidable](https://www.npmjs.com/package/formidable). The results they're seeing in the first example can be attributed to the use of the `{ extended: true }` flag of body-parser, I think. – Tobi Kremer Aug 30 '17 at 13:09