React Native使用Axios上传FormData时出现no multipart boundary was found错误求助
Hey there! I’ve run into this exact issue before, so let’s break down what’s going wrong and how to fix it quickly.
The Root Cause
When you manually set the Content-Type header to multipart/form-data, you’re overriding Axios’s ability to automatically generate a boundary string—this is the unique marker that tells the server where one form field ends and the next begins (including your file). Postman handles this automatically, which is why your request works there, but your Axios setup is missing this critical piece.
The Solution: Let Axios Handle the Content-Type
The simplest fix is to remove the manual Content-Type header from your request config. Axios detects when you’re sending a FormData object and automatically sets the correct multipart/form-data header with a valid boundary.
Before (Broken) Code:
axios.post('/your-endpoint', formData, { headers: { "Accept": "application/json", "Authorization": "Bearer your-token-here", "Content-Type": "multipart/form-data" // This line causes the problem } })
After (Fixed) Code:
axios.post('/your-endpoint', formData, { headers: { "Accept": "application/json", "Authorization": "Bearer your-token-here" // No manual Content-Type needed! Axios takes care of it } })
Edge Case: If You Need Custom Headers + FormData
If you have other custom headers to set and want to ensure the FormData headers are correctly included, you can use the getHeaders() method (supported in React Native’s FormData implementation) to merge the headers:
const formData = new FormData(); // Add your fields and file to formData as you were doing axios.post('/your-endpoint', formData, { headers: { ...formData.getHeaders(), // Gets the correct multipart/form-data header with boundary "Authorization": "Bearer your-token-here", "Accept": "application/json" } })
Why This Works
When Axios sends a FormData body without a manual Content-Type, it generates a header that looks like this:
Content-Type: multipart/form-data; boundary=----some-random-unique-string
This boundary is exactly what your Spring Boot server needs to parse the form fields and file correctly. By manually setting the header without the boundary, the server has no way to split up the request body, hence the error.
Give this a try—it should resolve the "no multipart boundary was found" error immediately!
内容的提问来源于stack exchange,提问作者Vidurajith Darshana

