ofetch icon indicating copy to clipboard operation
ofetch copied to clipboard

🙏 How to send `FormData` with `File` in node environment?

Open gyhyfj opened this issue 1 year ago • 1 comments

Describe the feature

In node environment (v20), I want to upload an image to OSS by posting FormData, I've tried several solutions, such as using form-data deps, but still failed.

It works well in web envrionment.

Additional information

  • [ ] Would you be willing to help implement this feature?

gyhyfj avatar Jan 02 '24 04:01 gyhyfj

https://www.npmjs.com/package/form-data

Lpmvb avatar Feb 28 '24 06:02 Lpmvb

use Native api just like fetch

ofetch.native('your_url', { method: 'POST', body: formData })

Loongphy avatar Aug 27 '24 04:08 Loongphy

Node.js supports built-in FormData since v18 powered by undici.

You can simply pass it as body and ofetch works with it.

PS: Using ofetch() and ofetch.native() should be equal. only difference is that ofetch.native returns full response object.

If you had further questions, please feel free to open a discussion with some of your code.

pi0 avatar Aug 28 '24 09:08 pi0