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

Already on GitHub? Sign in to your account

Pipe response inside callbacks #364

Closed
chrisjenx opened this Issue Nov 6, 2012 · 2 comments

Comments

Projects
None yet
2 participants

Hi im trying to pipe inside the response.

request(s,function (error, response, body) {
  if (!error && response.statusCode == 200) {
    body.pipe(fs.createWriteStream(ORGINAL_IMAGE_PATH+'image.jpg'));
  }
});

Of course this doesn't work. How would I pipe the body(image to the file).
fyi the folder does exist as request(s).pipe(...) works fine.

Cheers,
Chris

Owner

mikeal commented Nov 6, 2012

the callbacks waits for the entire response body to buffer, so that won't work. this is what you want.

var req = request(url)
req.on('response', function (resp) {
  if (resp.statusCode === 200) req.pipe(destination)
})

Splendid works like charm, worth adding to the readme? Many thanks!

@chrisjenx chrisjenx closed this Nov 6, 2012

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment