Zlatko
Zlatko

Reputation: 19569

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

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'?

Upvotes: 1

Views: 708

Answers (1)

twilson63
twilson63

Reputation: 441

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

Upvotes: 3

Related Questions