Unable to send multipart/form data with POST requests.
ripjar-mark opened this issue · comments
In xhr level 2, now widely supported , the XMLHttpRequest.send
method can be invoked with a formdata object, this will set the Content-Type
header automatically to multipart/form-data; boundary=----WebKitFormBoundaryxxxxxxxxxxx
.
Currently this library defaults the content type to application/x-www-form-urlencoded
see net/xhr.js l104. Setting this causes xhr to not override the content-type
header correctly and means that multipart/form-data
cannot be sent with POST
requests using this library.
Removing the default content-type fixes this.
@ripjar-mark Another option would be to set the content-type
header as a part of header
options when calling flow
.
jsonpipe.flow(url, {
"headers": {
"Content-Type": "multipart/form-data"
}
})
Will this work?
Unfortunately, this stops XmlHttpRequest from setting the the boundary as above, making the request invalid.
Are you setting the data
option as FormData
in your case? In that scenario, will the below logic work
if (addContentHeader && Object.prototype.toString.call(options.data) !== '[object FormData]') {
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
}
Well, if you were allowing FormData then I believe you would also have to handle "Blob, BufferSource, FormData, URLSearchParams, ReadableStream, or USVString " source, there is more information about the behaviours of XHR and the way it sets the headers in the fetch spec
The alternative would be allowing users to set a flag which disables the override?
Is it possible for you to issue a PR with probably a disableContentType
option? It may take a couple of days for me to get back to this project?