2

I want to download a image file to server first, then upload this file to other server.

If there is no download file step, it will be very simple

 var fs = require('fs');
 var path = '/tmp/test.png';
 var formData = {
   method: POST,
   url: 'http://127.0.0.1:3000',
   json: true,
   formData :{file: fs.createReadStream(path)}
 };
    request(formData, function(err, res, body){
   console.log("successful");
 });

However, for the situation with download image file;

I don't want to save the file first, through the format like request('http://google.com/doodle.png').pipe(fs.createWriteStream('doodle.png')). and then repeat fs.createReadStream.

Is there any method how can I get the file stream, so I can use the file stream directly to constitute formData;

ryu
  • 651
  • 9
  • 23
  • isn't request already handling upload of a file stream - https://github.com/request/request#streaming ; fs.createReadStream('file.json').pipe(request.put('http://example.com/obj.json')) – Jerome WAGNER Dec 07 '15 at 11:14
  • @JeromeWAGNER, yes, This is the step to handle file upload. I think 'file.json' should be a locale file. For my situation, I don't want to save a download file on the server, so there will not a file name. looking forward to your other advices. – ryu Dec 07 '15 at 11:24
  • @ryu so following the reasoning provided, did you try `request(formData).pipe(request.put(...))`? – robertklep Dec 07 '15 at 11:29
  • @robertklep, `request(formData).pipe(request.put(...))` should works. But in my issue, I think the point is how to get the file stream to process. I don't want to save file on the server, so I don't know how to do this except "fs.createReadStream" – ryu Dec 07 '15 at 11:37
  • @ryu `formData : request(downloadurl)`? – robertklep Dec 07 '15 at 11:40
  • @robertklep, I have tried, it doesn't work. – ryu Dec 07 '15 at 11:46

2 Answers2

3

This works for me:

var fs       = require('fs');
var request  = require('request');
var formData = {
  method   : 'POST',
  url      : 'http://127.0.0.1:3000',
  json     : true,
  formData : { file : request(downloadUrl) }
};

request(formData, function(err, res, body){
  if (err) throw err;
  console.log("successful");
});
robertklep
  • 198,204
  • 35
  • 394
  • 381
1

Following the request documentation, you can use

request.get(sourceUrl).pipe(request.post(targetUrl))

in this scheme, the data will flow from sourceUrl to targetUrl but will not need to be saved in a temporary file on the server.

cf https://github.com/request/request#streaming for more details.

Jerome WAGNER
  • 21,986
  • 8
  • 62
  • 77