Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable files to be set as value in FormData #79

Open
kiwiyou opened this issue Nov 4, 2021 · 6 comments
Open

Enable files to be set as value in FormData #79

kiwiyou opened this issue Nov 4, 2021 · 6 comments
Labels
enhancement New feature or request

Comments

@kiwiyou
Copy link
Contributor

kiwiyou commented Nov 4, 2021

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.

@nilslice nilslice added the enhancement New feature or request label Nov 4, 2021
@nilslice
Copy link
Contributor

nilslice commented Nov 5, 2021

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

@kiwiyou
Copy link
Contributor Author

kiwiyou commented Nov 6, 2021

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.

    /// Appends a new value onto an existing key inside a `FormData` object, or adds the key if it
    /// does not already exist.
    pub fn append(&mut self, name: &str, value: &str) -> Result<()> {
    self.0.append_with_str(name, value).map_err(Error::from)
    }
    /// Sets a new value for an existing key inside a `FormData` object, or adds the key/value if it
    /// does not already exist.
    pub fn set(&mut self, name: &str, value: &str) -> Result<()> {
    self.0.set_with_str(name, value).map_err(Error::from)
    }

  • In what way should we handle this From implementation? Specifically, can we return &dyn AsRef<&str> from File?

    impl From<HashMap<&dyn AsRef<&str>, &dyn AsRef<&str>>> for FormData {
    fn from(m: HashMap<&dyn AsRef<&str>, &dyn AsRef<&str>>) -> Self {
    let mut formdata = FormData::new();
    for (k, v) in m {
    // TODO: determine error case and consider how to handle
    formdata.set(k.as_ref(), v.as_ref()).unwrap();
    }
    formdata
    }
    }

@ghost
Copy link

ghost commented Nov 18, 2021

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.

@kiwiyou
Copy link
Contributor Author

kiwiyou commented Nov 19, 2021

@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.

@ghost
Copy link

ghost commented Nov 19, 2021

@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.

@kiwiyou
Copy link
Contributor Author

kiwiyou commented Dec 5, 2021

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants