elbywan / wretch

A tiny wrapper built around fetch with an intuitive syntax. :candy:
MIT License
4.79k stars 96 forks source link

Posting FormData to .NET Core Api #116

Closed emaborsa closed 2 years ago

emaborsa commented 2 years ago

Actually I have the same problem as the other user who posted this issue. My code:

const formData: FormData = new FormData();
formData.append("picture", files[0]);

wretch()
  .url('myApi')
  .formData(formData)
  .post()

When I run the code, debugging on chrome the payload results empty: wretch

On server side C# I receive the multipart request, but the form is empty: wretch2

elbywan commented 2 years ago

Hi @emaborsa,

.formData is a helper that takes an key/value map and will instantiate the FormData object and append values. It is not meant to receive an instance of FormData as an argument.

// Try this:

wretch()
  .url('myApi')
  .formData({
    pictures: files[0]
  })
  .post()

See here for another example.

The .formData documentation is not so great, I will definitely add something to make it clearer!