fetch - Missing boundary in multipart/form-data POST
Solution 1
The solution to the problem is to explicitly set Content-Type
to undefined
so that your browser or whatever client you're using can set it and add that boundary value in there for you. Disappointing but true.
Solution 2
I removed "Content-Type" and added 'Accept' to http headers and it worked for me. Here are the headers I used,
'headers': new HttpHeaders({
// 'Content-Type': undefined,
'Accept': '*/*',
'Authorization':
"Bearer "+(JSON.parse(sessionStorage.getItem('token')).token),
'Access-Control-Allow-Origin': this.apiURL,
'Access-Control-Allow-Methods': 'GET, POST, OPTIONS, PUT, PATCH, DELETE',
'Access-Control-Allow-Headers': 'origin,X-Requested-With,content-type,accept',
'Access-Control-Allow-Credentials': 'true'
})
Solution 3
fetch(url,options)
- If you set a string as
options.body
, you have to set theContent-Type
in request header ,or it will betext/plain
by default. If options.body is specific object like
let a = new FormData()
orlet b = new URLSearchParams()
, you don't have to set theContent-Type
by hand.It will be added automaticlly.- for
a
,it will be something like
multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW
as you see, the boundary is automaticlly added.
- for
b
, it isapplication/x-www-form-urlencoded;
- for
Solution 4
I had the same issue, and was able to fix it by excluding the Content-Type
property, allowing the browser to detect and set the boundary and content type automatically.
Your code becomes:
var formData = new FormData()
formData.append('myfile', file, 'someFileName.csv')
fetch('https://api.myapp.com',
{
method: 'POST',
body: formData
}
)
Solution 5
Add headers:{content-type: undefined}
browser will generate a boundary for you
that is for uploading a file part-and-part with streaming
if you are adding 'multiple/form-data' it means you should create streaming and upload your file part-and-part
So it is okay to add request.headers = {content-type: undefined}
Related videos on Youtube

Comments
-
James about 1 year
thanks for stopping by.
I want to send a
new FormData()
as thebody
of aPOST
request using the fetch apithe operation looks something like this
var formData = new FormData() formData.append('myfile', file, 'someFileName.csv') fetch('https://api.myapp.com', { method: 'POST', headers: { "Content-Type": "multipart/form-data" }, body: formData } )
the problem here is that the boundary, something like
boundary=----WebKitFormBoundaryyEmKNDsBKjB7QEqu
never makes it into the
Content-Type:
headerit should look like this
Content-Type:multipart/form-data; boundary=----WebKitFormBoundaryyEmKNDsBKjB7QEqu
when you try the "same" operation with a
new XMLHttpRequest()
, like sovar request = new XMLHttpRequest() request.open("POST", "https://api.mything.com") request.withCredentials = true request.send(formData)
the headers are correctly set
Content-Type:multipart/form-data; boundary=----WebKitFormBoundaryyEmKNDsBKjB7QEqu
so my question is,
how do I make
fetch
behave exactly likeXMLHttpRequest
in this situation?if this is not possible, why?
Thanks everybody! This community is more or less the reason I have professional success.
-
sww314 about 6 yearsusing fetch, I removed the Content-Type header and it worked.
-
Crysfel about 6 yearsUnbelievable!! I spent hours until I found this!!
delete result.headers['Content-Type'];
worked for me, thanks!! -
master_dodo almost 6 years@Crysfel Thanks.. deleting content type worked for me :P
-
sebnukem almost 6 yearsdoesn't work for me. Setting to undefined defaults to text/plain.
-
James almost 6 years@sebnukem which browser? I've never seen that happen, I'll admit though, I switched over to using Axios, it has a nicer api / behavior imo
-
sebnukem almost 6 years@James No problem with the browser. I found out that the HTTP library I was using coerced the body payload to a string.
-
James almost 6 years@sebnukem which http lib was that? this post is about
fetch
-
sebnukem almost 6 years@James, an in-house, proprietary lib
-
Green over 5 yearsVery well that you told us that
boundary
is added automatically. We have all known that before. -
Green over 5 yearsBut why is it so?
-
James over 5 years@Green Apparently you can define an arbitrary boundary -> stackoverflow.com/questions/3508338/… -- I just didn't, I only set the content type, but that is only half the job. On the other hand, If you just leave the content type alone, the browser can infer it, and set the type + the boundary to an appropriate compliant, random string for you.
-
Ahab over 5 yearsNeither setting the content-type to undefined, nor deleting the content-type before making the POST with fetch did it for me...
-
James over 5 years2 years later, I'd recommend using something like axios unless you absolutely need isomorphic fetch - over the years I've found fetch to be more of a nuisance than a help
-
Oscar Calderon about 5 yearsSetting value to undefined didn't work for me, but removing header worked :)
-
jaeyow about 5 yearsexplicitly setting
Content-Type
toundefined
worked for me -
Eugenijus S. almost 4 yearsyour explanation makes much more sense, thank you! I managed to get my code working!
-
iVoidWarranties over 3 yearsNo other solutions worked for me. After adding Accept: / header, everything worked like a charm.
-
Scribblemacher over 2 yearsSetting the Content-Type header to undefined seemed to come across for me as "Content-Type": undefined in the request itself. not defining it worked.