Ky: Sending form-data

Created on 28 Jan 2019  路  8Comments  路  Source: sindresorhus/ky

Issuehunt badges

Are there any docs?

szmarczak earned $40.00 by resolving this issue!

Rewarded on Issuehunt documentation

Most helpful comment

Sending form data in Ky is identical to fetch. That is to say:

  • Passing a FormData instance as the body causes the Content-Type header to default to multipart/form-data.
  • You can still override the 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.
  • If you want to send the data in application/x-www-form-urlencoded format, then you will need to either:

    • Encode the data with URLSearchParams instead of using FormData (again the Content-Type will be set automatically), or...

    • Call 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.

All 8 comments

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-Type header when body is a FormData instance?

Maybe use warning instead of error will be better?

@LitoMore Why?

Sending form data in Ky is identical to fetch. That is to say:

  • Passing a FormData instance as the body causes the Content-Type header to default to multipart/form-data.
  • You can still override the 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.
  • If you want to send the data in application/x-www-form-urlencoded format, then you will need to either:

    • Encode the data with URLSearchParams instead of using FormData (again the Content-Type will be set automatically), or...

    • Call 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

  • :moneybag: Total deposit: $40.00
  • :tada: Repository reward(0%): $0.00
  • :wrench: Service fee(10%): $4.00
Was this page helpful?
0 / 5 - 0 ratings

Related issues

luxferresum picture luxferresum  路  7Comments

Splact picture Splact  路  7Comments

sarneeh picture sarneeh  路  6Comments

kirillgroshkov picture kirillgroshkov  路  3Comments

ShivamJoker picture ShivamJoker  路  5Comments