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
Duplicate of #771 and #774
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,
await pipeline(
fs.createReadStream('testindoormap.zip'),
got.stream.post(uploadDrawPackageURL).on('response', response => {
console.log("never reach this line of code")
console.log(response.headers);
}).resume()
)
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({
uri: uploadDrawPackageURL,
body: file
},(err, httpResponse, body)=>{
console.log(httpResponse)
});
Hmmm. That sounds like a bug, the response event should be emitted. Can you create a RunKit example please? A repository works too.
I have a small test repository for your try
https://github.com/leolumicrosoft/testGot.git
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.
You can just:
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 options.body can be a stream.
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 body to a stream because I'm using new tar.Parse() which Got rejects as not a valid Readable stream (I'm guessing it does an instanceof check that fails?) but it can be piped fine, so I need to use got.stream to pipe the request body in. But I really just want a simple JSON body, and cannot figure out how to access the body at all in that mode. The example above with new stream.PassThrough() still didn't give me any body. responseType is also ignored per documentation.
@felixfbecker See #1503 - your issue is almost the same.
Most helpful comment
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.
You can just:
Because
options.bodycan be a stream.