serviejs / popsicle

Simple HTTP requests for node and the browser
MIT License
246 stars 19 forks source link

How to set the filename using multipart form data and raw stream of buffer? #91

Closed sinedied closed 4 years ago

sinedied commented 7 years ago

Sending form data works well when using streams created with fs but I'm actually trying to upload data from a Node buffer, and unfortunately that doesn't work well.

It seems that I'm missing the part where the filename is specified in the form data, and I found nothing in your source code to help. When transmitting form data, it should look like this:

'Content-Disposition: form-data; name="file"; filename="myfile.ext" [...]

What I'm actually missing is how to set the filename="myfile.ext" part without using a stream from fs?

sinedied commented 7 years ago

In any case someone needs it, here is the solution:

popsicle.form only support uploading files using standard file stream, to work with buffer or other stream type you need to use form-data directly:

    const form = new FormData();
    form.append('file', mystream, {
      filename: myfilename,
      knownLength: myfile.length
    });

It would be nice if popsicle allowed to set those options though.

blakeembrey commented 7 years ago

@sinedied popsicle.form returns an instance of form-data, so it allows all the same features to work as you've just used. Just call const form = popsicle.form() and append after it.

blakeembrey commented 7 years ago

I'd love to improve the README with this note! 😄

Obapelumi commented 4 years ago

popsicle.form is undefined in popsicle 12.0.1

blakeembrey commented 4 years ago

@Obapelumi You can use form-data directly nowadays. This was largely before form-data has a browser aliased version (e.g. https://github.com/form-data/form-data/blob/d7026253e728af9568503dc3dc55cd1a566605e0/package.json#L11).