37

Notice: only when I use form-data body form in Postman (which is the form I have to use because I want to send files beside text fields), I get:

Error: Multipart: Boundary not found.

when I use x-www-form-urlencoded everything is ok. (ofcourse when body-parser is used as middleware)

This is Request Content: (made by Postman)

POST /test HTTP/1.1
Host: localhost:3000
Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW
Cache-Control: no-cache
Postman-Token: 61089a44-0b87-0530-ca03-a24374786bd1

------WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="test"

a simple word
------WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="data"

good
------WebKitFormBoundary7MA4YWxkTrZu0gW--

index.js:

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

var multer = require('multer');
var upload = multer();

app.post('/test', upload.array(), function (req, res, next) {
    console.log(req.body.test);
    console.log(req.body);
});

app.listen(3000, function () {
    console.log('app started');
});
Kalle Richter
  • 7,146
  • 22
  • 65
  • 152
  • if you are having this issue in react native check this answer [enter link description here](https://stackoverflow.com/a/71205192/13989650) – Shazil Sattar Feb 21 '22 at 11:15

4 Answers4

103

I found the solution. I only had to prevent Postman to send a Content-Type header. So I just removed it from request headers.

Kalle Richter
  • 7,146
  • 22
  • 65
  • 152
6

To give some insight on why that is happening,

When using content type multipart/form-data in any HTTP request, you can add a boundary information alongside the Content-Type specification like:

Content-Type: multipart/form-data; boundary=MyBoundary

You can replace MyBoundary with any string of your liking.

Then you will have to encode your form data (name=Abebe&age=5) as:

--MyBoundary
Content-Disposition: form-data; name="name"

Abebe
--MyBoundary
Content-Disposition: form-data; name="age"

5
--MyBoundary--

For more info read this StackOverflow question and answer

Leone
  • 2,862
  • 3
  • 23
  • 26
3

For JMeter and postman remove Content-Type from header.

it will resolve your issue.

3

I am going to expand a little bit on user9150719 for those who are having the same issue with the frontend side of things and are wondering where to remove the headers.

I had the same issue; I was trying to post from an Angular app to my Nodejs server. my post request included raw data and a file input. So I was thinking FormData()

Angular Service

//Declare header variables. 

formDataHeader = {
        headers: new HttpHeaders({
          Accept: 'application/json',
          'Access-Control-Allow-Origin': '*',
          'Content-Type': 'multipart/form-data',
          Authorization: 'Bearer ' + this._myService.getToken()
        })
      };

//Post function to Nodejs server
    addNewContact(contact: FormData): any {

        return this._httpClient.post(
          environment.apiBaseUrl + '/contacts', // POST /api/contacts
          (contact), // contact data,
          this.formDataHeader
        );
    }

My formData was setup properly. I was able to get all the data, but the problem is that I had setup couple headers in my request that resulted in what user9150719 was experiencing.

My solution was to simplify my headers to this:

formDataHeader = {
        headers: new HttpHeaders({
          Authorization: 'Bearer ' + this._myService.getToken()
        })
      };

Another important thing to point out is that I didn't need to set the enctype="multipart/form-data" on my <form></form> tag.

Even though I had an httpInterceptor setup (I don't think it is working properly), I still needed to add the Authorization header on all my requests, but all other headers were resulting in my api call to return unexpected results.

Finally I think (but I am not entirely sure) that the reason why I didn't need to setup extra headers, is because in my NodeJS server, I already configured what headers to expect.

Node.JS Server

// app.js

app.use('/public/uploads', express.static('uploads'));
app.use('/public', express.static('public'));
app.use(express.static(path.join(__dirname, 'public')));
app.use(cors());
app.use((req, res, next) => {
    res.header('Access-Control-Allow-Origin', '*');
    res.header('Access-Control-Allow-Headers', 'x-www-form-urlencoded, Origin, X-Requested-With, Content-Type, Accept, Authorization, *');
    if (req.method === 'OPTIONS'){
        res.header('Access-Control-Allow-Methods', 'GET, PUT, POST, PATCH, DELETE, OPTIONS');
        res.setHeader('Access-Control-Allow-Credentials', true);
        return res.status(200).json({});
    }
    next();
});
app.use(bodyParser.urlencoded({limit: '50mb', extended: true}));
app.use(bodyParser.json({limit: '50mb', extended: true}));

So I think that if your server is setup to handle certain types of headers (Content-Type, Authorization, Origin, etc.), You don't necessarily need to set those headers again on your frontend when you send your request to the server. There are certain exceptions, such Authorization which in certain cases need to be set; probably because they carry some data in the form of token or something in that regards.

I hope this helps someone out there!

AllJs
  • 1,612
  • 4
  • 24
  • 44