Are there any docs?
szmarczak earned $40.00 by resolving this issue!
- Checkout the Issuehunt explorer to discover more funded issues.
- Need some help from other developers? Add your repositories on Issuehunt to raise funds.
In case anyone has any trouble:
const formData = new FormData();
formData.append("file", file);
formData.append("hash", hash);
const headers = {
/* DO NOT SET THIS, WILL NOT WORK "Content-Type": "multipart/form-data", */
};
ky.post(url, {
body: formData,
headers
});
Are there any docs?
Currently no, but I think it would be nice to have things clearified :)
@szmarczak Maybe we should throw an error if someone tries to set the Content-Type header when body is a FormData instance?
Maybe we should throw an error if someone tries to set the
Content-Typeheader whenbodyis aFormDatainstance?
Maybe use warning instead of error will be better?
@LitoMore Why?
Sending form data in Ky is identical to fetch. That is to say:
FormData instance as the body causes the Content-Type header to default to multipart/form-data.Content-Type header if need be, but I'm not aware of any case where this would be useful when using FormData, since the whole point of FormData is to encode the data in that specific format.application/x-www-form-urlencoded format, then you will need to either:URLSearchParams instead of using FormData (again the Content-Type will be set automatically), or...form.submit() (i.e. use the DOM instead of Ky or fetch)As for whether Ky should throw an error for attempting to override the Content-Type header while using FormData, I'm +0.5 in favor of that since it's probably a mistake. One downside is that FormData would be a new global that some users may need to polyfill even if they aren't using FormData themselves. But at least we can add the polyfill to ky-universal to make it easier.
And to be clear, @baranovxyz your code snippet should work regardless of whether headers is undefined, an empty object, or if you specify 'Content-Type': 'multipart/form-data' explicitly. If it doesn't, then that's a bug, and I don't see what could cause it.
@issuehunt has funded $40.00 to this issue.
@sindresorhus has rewarded $36.00 to @szmarczak. See it on IssueHunt
Most helpful comment
Sending form data in Ky is identical to
fetch. That is to say:FormDatainstance as thebodycauses theContent-Typeheader to default tomultipart/form-data.Content-Typeheader if need be, but I'm not aware of any case where this would be useful when usingFormData, since the whole point ofFormDatais to encode the data in that specific format.application/x-www-form-urlencodedformat, then you will need to either:URLSearchParamsinstead of usingFormData(again theContent-Typewill be set automatically), or...form.submit()(i.e. use the DOM instead of Ky orfetch)As for whether Ky should throw an error for attempting to override the
Content-Typeheader while usingFormData, I'm +0.5 in favor of that since it's probably a mistake. One downside is thatFormDatawould be a new global that some users may need to polyfill even if they aren't usingFormDatathemselves. But at least we can add the polyfill to ky-universal to make it easier.And to be clear, @baranovxyz your code snippet should work regardless of whether
headersisundefined, an empty object, or if you specify'Content-Type': 'multipart/form-data'explicitly. If it doesn't, then that's a bug, and I don't see what could cause it.