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

Accept streams as body #2075

Closed
petkaantonov opened this issue Feb 13, 2016 · 12 comments
Closed

Accept streams as body #2075

petkaantonov opened this issue Feb 13, 2016 · 12 comments
Labels

Comments

@petkaantonov
Copy link

If I want to send the request body e.g. gzipped, I currently need the return value and pipe the gzipstream there. However I only have access to the return value when using callback API but we are normally using promise API where the return value "slot" is taken by the promise and we cannot interact with the stream.

I propose extending the body option to accept stream which would do this for me so that I don't need to waste the return value "slot". Any errors should be redirected to the callback error argument.

@simov
Copy link
Member

simov commented Feb 15, 2016

@request/promises can you help with this issue?

@simov simov added the Promises label Feb 15, 2016
@petkaantonov
Copy link
Author

Note I am using request through promisifying the callback api, so changes to any promise api doesn't address this.

This is also something that could be useful even without using promises, so I don't think the promise label is entirely accurate.

@analog-nico
Copy link
Member

Hi @petkaantonov if I understand it correctly you currently use:

gzipStream.pipe(request.post(endpoint));

...whereas this wouldn't work:

gzipStream.pipe(request.post(endpoint, function (err, response, body) {
    // Callback is provided through promisification.
    // Thus cannot have custom code that interacts with the stream here.
}));

...and you propose this:

request.post({
    uri: endpoint,
    body: gzipStream // Passing a stream instead of just JSON
}, function (err, response, body) {
    // err is either an error of the request or an error message of the gzipStream stream
});

Correct?

@petkaantonov
Copy link
Author

Yes I want body: gzipStream to work. But gzipstream.pipe(request.postAsync()) doesn't work since postAsync returns a promise not a stream.

@simov
Copy link
Member

simov commented Feb 15, 2016

@petkaantonov can you provide a full pseudo code on what you are trying to achieve.

@petkaantonov
Copy link
Author

request({
   ...
   body: gzipStream
   ...
});

Should work the same as if I did gzipStream.pipe(request({...}). Everything else is just reasoning why I need this and not relevant per se.

@simov
Copy link
Member

simov commented Feb 15, 2016

That seems doable. What happens if you want to stream the response as well? In this case your only choice would be to buffer it inside the callback, though that's probably the case when using Promises anyway?

@petkaantonov
Copy link
Author

Well in practice when request payload is big, the response payload is small. And vice versa. So it's not a problem to buffer the short acknowledgment response.

@simov
Copy link
Member

simov commented Feb 15, 2016

What I'm trying to say is that the user should be able to pass not only the input stream, but the output one as well. Buffering by default might not be what you want.

So body: stream should be implemented in request, and other_option: output should be implemented in request-promise since it's already a wrapper and it deals with promises.

When the response event is emitted the response should be piped to whatever output stream you pass to request-promise and the promise should be resolved on output stream end event - that's the case when you don't want to buffer the entire response body in a callback.

@petkaantonov
Copy link
Author

Yes I am not against that of course.

btw I have no affiliation with request-promise. I am using the vanilla request via bluebird's promisify function which does automagic api translation.

@simov
Copy link
Member

simov commented Feb 15, 2016

@petkaantonov take a look at #2079

@analog-nico
Copy link
Member

@simov I covered your suggestion in request/request-promise#90

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

No branches or pull requests

3 participants