-
-
Notifications
You must be signed in to change notification settings - Fork 935
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
Detach response stream in Promise mode #774
Comments
Duplicate of #771 |
This isn't really a duplicate of #771 because that issue just wanted the headers. I do want the body, I just want to pipe it from the request like I modified the workaround given in #771, but that gives me an empty body when I try to pipe the response to something. Plus it's even uglier than the workaround in my initial comment. const getResponse = () =>
new Promise((resolve, reject) => {
const promise = got('http://example.com').on('response', response => {
resolve(response)
promise.cancel()
})
promise.then(response => resolve(response)).catch(reject)
})
;(async () => {
const res = await getResponse()
res.pipe(fs.createWriteStream('got-test.html'))
// got-test.html is empty 😕
})() In short, I want this: const res = await got('http://example.com', {parseBody: false})
res.pipe(fs.createWriteStream('got-test.html')) If you don't think this is a valuable feature, that is fine, but it is different than just wanting to grab the headers. |
Hmm... As I see it you want to combine Promise and Stream mode. Currently, that's not possible. So either you need to do: const combined = async (...args) => {
const stream = got.stream(...args);
const response = await pEvent(stream, 'response');
return {stream, response};
}; or got('https://example.com').on('response', response => {
response.pipe(...); // Pipe first, then silently reject further pipes
response.on('newListener', (event, listener) => {
if (event === 'data') {
response.removeListener(event, listener);
}
});
}); |
Thanks for the work arounds. This was meant as a feature request, so if you're not willing to discuss this as a new feature, then you can keep it closed, but I was hoping for more of a discussion. |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
Also it won't work if the response is cached, beware of that. So you need to disable cache. The stream approach seems to be the best option. |
Closing because lack of activity. This is not an actual problem. |
What problem are you trying to solve?
Got stores the body of the response in the
body
property. This is great if you're working with relatively small payloads, like text, but it's not great for large files because it eats up your memory.Of course, you can use
got.stream
, but then you just get a stream instead of the response object. To get the response, you can listen to theresponse
event, but then you've moved from promises to callbacks.Describe the feature
Having a feature that resolves with the response, but still lets you process the body as a stream would be great. Since
IncomingMessage
is a stream, maybegot.stream
can just return the response instead of just a stream.Checklist
The text was updated successfully, but these errors were encountered: