Error: Multipart: Boundary not found in Express.js [Solved]

avatar
Borislav Hadzhiev

Last updated: Apr 5, 2024
4 min

banner

# Error: Multipart: Boundary not found in Express.js [Solved]

The Express.js "Error: Multipart: Boundary not found" occurs when you explicitly set the Content-Type header in your POST request.

To resolve the error, remove the Content-Type header, so that your browser or Postman App can generate it automatically.

error multipart boundary not found

When you set the Content-Type of an HTTP request to multipart/form-data, the boundary parameter has to be set in the Content-Type header.

Your browser, HTTP library or the Postman application will automatically set the Content-Type header and its boundary parameter correctly.

If you've set the Content-Type header in your code (or in Postman), remove it so you don't override the correctly set value.

For example, suppose that you have the following Express.js server.

index.js
import express from 'express'; import multer from 'multer'; // ๐Ÿ‘‡๏ธ if you use CommonJS require() // const express = require('express') // const multer = require('multer') const upload = multer({dest: 'uploads/'}); const app = express(); app.post( '/profile', upload.single('avatar'), function (req, res, next) { // req.file is the `avatar` file // req.body will hold the text fields, if there were any console.log(req.file); console.log(req.body); res.send('success'); }, ); const port = 5000; app.listen(port, () => { console.log(`Example app listening on port ${port}`); });
The code for this article is available on GitHub

Make sure you have express and multer installed.

shell
npm init -y # ๐Ÿ‘‡๏ธ with NPM npm install express multer # ๐Ÿ‘‡๏ธ or with YARN yarn add express multer

You can start your server with npx nodemon index.js.

shell
npx nodemon index.js

If you use Postman:

  1. Specify POST as the request method.
  2. Set http://localhost:5000/profile as the URL.
  3. Make sure to select form-data from the dropdown menu.
  4. In the key-value pair section, set the type of the Key to File and select your file.

set request body correctly in postman

  1. Click on the Headers tab and make sure the Content-Type header is not set.

make sure content type is not set

If I issue the POST request and check my Express.js server, I can see that the file has been uploaded successfully.

file uploaded successfully

The same is the case when using fetch, axios or a direct request using an HTML form:

  • Remove the Content-Type header from the request so that it gets automatically generated.

If you explicitly set the Content-Type header, it won't have the boundary property set and your request won't be parsed correctly from the server.

The following manually specified Content-Type header is incorrect because it doesn't have the boundary property.

shell
Content-Type: multipart/form-data;

Instead, your browser will generate something that looks like this:

shell
Content-Type: multipart/form-data; boundary=---------------------------WebKitFormBoundaryh9UF2hFjspgvNh9T--

If you use axios and still get the error even after removing the Content-Type header, try to set the header with the boundary property.

index.js
axios.post(`https://example.com/profile`, formData, { headers: { 'Content-Type': `multipart/form-data; boundary=${formData.getBoundary()}`, }, });
The code for this article is available on GitHub

The boundary property can be set to anything in the Content-Type header.

You can also manually set the property, however, it is best to let the browser or the HTTP client generate it for you.

For example, if you send a POST request with Content-Type set to application/x-www-form-urlencoded with the following data:

shell
name=BobbyHadz&salary=500

The server knows the parameters are separated by an ampersand & and knows how to parse the string.

The boundary property is used to tell the server how the parameters are separated when using the multipart/form-data content type.

Here is an example of setting the boundary property to ---XYZ.

shell
Content-Type: multipart/form-data; boundary=---XYZ -----XYZ Content-Disposition: form-data; name="name" BobbyHadz -----XYZ Content-Disposition: form-data; name="salary" 500 -----XYZ--

The ---XYZ value of the boundary property indicates to the server how the parameters (name and salary) are separated, so the server knows how to parse them.

When the Content-Type header is set to multipart/form-data, the boundary parameter has to be set, otherwise, the "Multipart: Boundary not found" error is raised.

The boundary parameter in the example above is ---XYZ.

The boundary delimiter line is defined by prepending two -- hyphens to the value of the boundary parameter (becomes -----XYZ).

shell
-----XYZ Content-Disposition: form-data; name="name"

Boundary delimiters cannot be longer than 70 characters, not counting the two leading hyphens.

The last boundary delimiter line is followed by two -- more hyphens after the boundary parameter value.

shell
-----XYZ--

This is used to indicate that no further body parts will follow.

If for some reason, your browser or HTTP client doesn't automatically set the Content-Type header correctly (with a boundary property) even after you've removed the header, then try to set it manually and rerun the HTTP request.

shell
Content-Type: multipart/form-data; boundary=---XYZ
The code for this article is available on GitHub

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev