Closed tonisostrat closed 2 years ago
Hey @tonisostrat,
Is there some part of the documentation that I've missed or is this a genuine limitation?
Passing the files
array as a value of the formData
object argument should do the trick.
const files = getCachedFiles();
getWretch()
.url("")
.formData({ files })
.post();
Thank you for the prompt reply!
Unfortunately passing the array in as-is does not work - if you take a look at my original post that is exactly what I did. Using the array generates a request body where the field name is files[]
instead of just files
.
I have put together a quick demonstration here. The queries obviously fail but if you open up your browser's console you can inspect the body content and the difference becomes evident.
Unfortunately passing the array in as-is does not work - if you take a look at my original post that is exactly what I did. Using the array generates a request body where the field name is files[] instead of just files.
Ah I'm sorry my bad I replied too quickly 🤦.
So yeah in this case this is a limitation and you will need to use Formdata.append
+ .body
instead of the .formData
helper.
Gotcha, at least I know I'm not going crazy :)
Is there any chance you'll consider implementing this functionality? The helper method is great and really cuts down on LoC compared to instantiating the FormData
object, especially as it doesn't even have a fluent interface..
Is there any chance you'll consider implementing this functionality? The helper method is great and really cuts down on LoC compared to instantiating the FormData object, especially as it doesn't even have a fluent interface..
So if I recall correctly the reason why the brackets ([]
) are auto-appended for array properties is that I followed the MDN documentation stating that some languages like PHP are using this convention.
In retrospective I think that was a bad move, but changing it would be breaking.
I'm definitely planning to release 2.0 at some point and rewrite the whole lib, I just cannot tell when this will happen yet. But when it does I'll make sure to include this change 😉.
Should be fixed with the v2.
I seem to have run into a limitation of the
formData()
builder method. Our (Spring) back-end expects an array-type field containing all user-submitted files. It's trivial to achieve with the defaultFormData
object:which produces the following multipart payload (only the relevant lines have been kept):
but it does not work with
wretch
's custom helper method:which results in the following payload:
and the data on the back-end is
null
.Obviously it's also impossible to add the files separately under the same key as the input for the method is an
object
:Is there some part of the documentation that I've missed or is this a genuine limitation?