How to name a file parameter when uploading an image with node request?

Problem

I'm using request module (https://npmjs.org/package/request) to fetch an image and I want to pipe it to a third-party api.

Something like this:

request('http://original.image.url/image.png').pipe(request.post('http://some.api/upload'));

Now, I'm not sure how to do a few things.

  1. How do I name the post parameter?

  2. How to add other form fields, like API key and other data?

  3. How to make the header of my post request 'multipart/formdata'?

Problem courtesy of: Zlatko

Solution

See https://github.com/mikeal/request#forms

For multipart/form-data we use the form-data library by @felixge. You don’t need to worry about piping the form object or setting the headers, request will handle that for you.

var r = request.post('http://service.com/upload')    
var form = r.form()    
form.append('my_field', 'my_value')    
form.append('my_buffer', new Buffer([1, 2, 3]))     
form.append('my_file', fs.createReadStream(path.join(__dirname, 'doodle.png'))     
form.append('remote_file', request('http://google.com/doodle.png'))     

Also see

https://github.com/felixge/node-form-data

Solution courtesy of: twilson63

Discussion

There is currently no discussion for this recipe.

This recipe can be found in it's original form on Stack Over Flow.