Skip to content
New issue

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

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Piping response of POST not writing to writeStream #684

Closed
johnmanong opened this issue Jun 23, 2015 · 6 comments
Closed

Piping response of POST not writing to writeStream #684

johnmanong opened this issue Jun 23, 2015 · 6 comments

Comments

@johnmanong
Copy link

I might be crazy, but the below code snippet does not seem work for me. res.body is a valid buffer and I can manually write to the stream with it, but I'm confused as to why pipe doesn't seem to be working.

// in a node env

var outStream = fs.createWriteStream('output.png');
  var req = superagent.post('http://pngcrush.com/crush')
                      .attach('input', 'filename.png');

  req.end(function(err, res) {
    res.pipe(outStream);  // why you no work, outStream still empty
  });
@mijie1023
Copy link

req.end(function(err, res) {
// console.log(res.body);

fs.writeFile('test.png', res.body, function(err) {
console.log('Error: ' + err);
});
});

@Enet4
Copy link

Enet4 commented Jul 20, 2016

Bumping this dormant issue because an SO question emerged with the same problem. I've just answered it too. Basically, right now one should pipe the request object instead of the obtained response.

Unless you wish to consider improving the documentation on how a response object should not be treated as a readable stream or actually making that part of the public API, this issue can be closed.

@kornelski
Copy link
Contributor

The .end() callback is called after piping has ended and is no longer possible.

You can't use both .end() and .pipe(). Only one of them is possible at a time.

@Enet4
Copy link

Enet4 commented Jul 20, 2016

The .end() callback is called after piping has ended and is no longer possible.

You can't use both .end() and .pipe(). Only one of them is possible at a time.

Note that neither I nor the questions suggest doing both end() and pipe() on the request, but rather end the request and pipe the response, which is actually not that far out to assume the possibility. A small statement in this section on the subject would be nice to have.

@kornelski
Copy link
Contributor

kornelski commented Jul 20, 2016

When you call .end() then superagent takes over response stream pipe, collects all of its data in res.body, and waits for the pipe to finish sending data.

The callback in .end() is called after response stream pipe has finished and closed. At this point it's guaranteed that it's not possible to pipe anything anymore, because all pipes have ended and closed.

If you want to send data from the end callback, you must use expressJsResponse.end(superagentResponse.body), because at this point in time res.body is the only place where the data exists.

@kornelski
Copy link
Contributor

cc #1188

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants