[Fixed] express (using multer) Error: Multipart: Boundary not found, request sent by POSTMAN – Express

Photo of author
Written By M Ibrahim
angular-forms express multer node.js

Quick Fix: Within Postman, navigate to the "Headers" tab of the request. Locate the "Content-Type" header and remove it. This action prevents Postman from sending the Content-Type header, allowing multer to correctly process the multipart data.

The Problem:

You are using multer in your Express.js application to handle multipart/form-data requests. However, when you use the form-data body form in Postman, you encounter the error ‘Multipart: Boundary not found.’ This issue occurs because you have not specified the boundary string correctly in your request. You need to ensure that the boundary you provide in your request matches the boundary specified in your multer configuration.

The Solutions:

Solution 1: Remove `Content-Type` header

The issue in this case was that Postman was sending a `Content-Type` header along with the request, which was causing multer to fail. To resolve this, the solution was to prevent Postman from sending the `Content-Type` header. This can be done by removing the `Content-Type` header from the request headers in Postman.

Solution 2: Use of Boundary in Content-Type of POST request

When forming a POST request with multipart/form-data content type, the Boundary must be explicitly specified as stated in the Content-Type header.

The request should include the boundary parameter in the Content-Type header and the data should be encoded accordingly.

For instance:

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

In your request, the boundary should be specified and the message body needs to be encoded to follow the correct structure. Refer to the given links to gain more knowledge about boundary in multipart/form-data content type.

Solution 3: Setting the header manually

If you are using the fetch method, you can set the Content-Type header manually to multipart/form-data. This will ensure that the correct boundary is set and the request is sent correctly.

fetch("http://localhost:4000/upload_files", {
  method: 'POST',
  body: formData,
  headers: {
    "Content-Type": "multipart/form-data"
  }
})

Alternatively, you can remove the Content-Type header altogether. The fetch API will automatically set the correct headers for a multipart/form-data request if the FormData object is used as the body.

fetch("http://localhost:4000/upload_files", {
  method: 'POST',
  body: formData
})

Solution 4: Remove `Content-Type` header for tools like JMeter and Postman

If you are making requests using tools like Postman or JMeter, you should omit the `Content-Type` header in your requests to resolve the `Error: Multipart: Boundary not found` issue.

By omitting the Content-Type header, these tools will automatically handle the multipart/form-data encoding and will set the appropriate boundaries in the request. This will allow your Node.js server to correctly parse the multipart/form-data data and retrieve the text fields and files as expected.

Solution 5: Angular 6 API – Fix Error: Multipart: Boundary not found for form-data POST Request

The issue of "Multipart: Boundary not found" error while sending a form-data request using Angular and Express.js can occur due to improper headers being set in the request. To resolve this, simplify the headers in the Angular service and ensure that only necessary headers are included.

Simplified Angular Service Headers:

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

Node.js Server Configuration:

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

Angular Form Tag:

<form enctype="multipart/form-data">
  ...
</form>

By following these steps, you can ensure that the proper headers are set on both the frontend (Angular) and the backend (Express.js), resolving the “Multipart: Boundary not found” error and allowing successful form-data requests.