-
-
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
get http response when using stream API to upload file #1514
Comments
I can not figure out how 771 or 774 give me a solution. Can you directly give a brief sample code for my purpose? Note I am uploading file to a server and want to get the responds from server, NOT downloading. What i tried, It might help you understand my intention by a working code I did using "request" library var file = fs.createReadStream('testindoormap.zip') var req = request.post({ |
Hmmm. That sounds like a bug, the |
I have a small test repository for your try there are two JS, one is using GOT. The other is using request and this one gives server response. Please have a look. Thanks. |
We discusses this a lot in #1481 TL; DR: If you care about the response from the server you need to read the response stream, here's a small example: const { pipeline } = require('stream/promises');
const fs = require('fs');
const got = require('got');
const request = got.stream.post('http://127.0.0.1:8080/');
request.on('response', () => {
console.log('response');
});
(async () => {
await pipeline(
fs.createReadStream('testindoormap.zip'),
request,
new stream.PassThrough()
);
console.log('Pipeline END');
})(); |
I wrote the two examples in order to give some code to explain how to handle streams, but what I think you (@leolumicrosoft) need is much simplier. const fs = require('fs');
const got = require('got');
(async () => {
const response = await got.post('http://127.0.0.1:8080/', {
body: fs.createReadStream('testindoormap.zip'),
retry: 0
});
console.log(response.headers);
console.log(response.body);
})(); Because |
Ok. I did not realize I do not need to use stream pipeline for uploading read stream, instead I can directly post with body as the read stream. It solves my problem, thanks. |
I still cannot figure out how to get the response body in stream mode. I cannot set |
@felixfbecker See #1503 - your issue is almost the same. |
I can follow sample code to successfully upload file, like below
...
await pipeline(
fs.createReadStream('index.html'),
got.stream.post('https://sindresorhus.com')
);
...
but I do not know how to get the response from http server, such as the status code and response body. Can you give me a sample code? Thanks
The text was updated successfully, but these errors were encountered: