workers-rs icon indicating copy to clipboard operation
workers-rs copied to clipboard

Enable files to be set as value in `FormData`

Open kiwiyou opened this issue 4 years ago • 6 comments

It's quite strange to have FormData::get returning FormEntry with FormData::set and FormData::append accepting only &str. It would be great if we can insert File or something like it directly into FormData.

kiwiyou avatar Nov 04 '21 12:11 kiwiyou

This is a great idea. Is this something you would like to work on?

nilslice avatar Nov 05 '21 21:11 nilslice

I'd love to. But there seems to be some issues that should be resolved first:

  • Should we replace current append and set to File-aware variant or create a new one? Replacing them will be a breaking change. https://github.com/cloudflare/workers-rs/blob/07bf913c21e03558af8c092d5dc53ef148ad8ec7/worker/src/formdata.rs#L79-L89

  • In what way should we handle this From implementation? Specifically, can we return &dyn AsRef<&str> from File? https://github.com/cloudflare/workers-rs/blob/07bf913c21e03558af8c092d5dc53ef148ad8ec7/worker/src/formdata.rs#L103-L112

kiwiyou avatar Nov 06 '21 11:11 kiwiyou

I just ran into this issue after creating most of my project and I've been in disbelief that this isn't available. Using a worker as a switchboard to send and receive files just makes sense to me; the functionality as a service is severely limited without this.

ghost avatar Nov 18 '21 18:11 ghost

@sfbr As a workaround, you can manually build a FormData body and set appropriate headers to send multipart data. crate multipart will do this for you.

kiwiyou avatar Nov 19 '21 11:11 kiwiyou

@kiwiyou Are you suggesting to send the request using that crate or to create the byte array and use it with this API? Additionally, I don't see where I would send FormData even if I did create it.

ghost avatar Nov 19 '21 14:11 ghost

Sorry for late response. I meant creating a form data, encoding it into a byte array, and attaching it as a request body. FormData cannot be directly sent with current API as you said.

kiwiyou avatar Dec 05 '21 15:12 kiwiyou