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

get http response when using stream API to upload file #1514

Closed
leoluKL opened this issue Oct 28, 2020 · 9 comments
Closed

get http response when using stream API to upload file #1514

leoluKL opened this issue Oct 28, 2020 · 9 comments

Comments

@leoluKL
Copy link

leoluKL commented Oct 28, 2020

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

@szmarczak
Copy link
Collaborator

Duplicate of #771 and #774

@leoluKL
Copy link
Author

leoluKL commented Oct 29, 2020

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)
});

@szmarczak
Copy link
Collaborator

Hmmm. That sounds like a bug, the response event should be emitted. Can you create a RunKit example please? A repository works too.

@leoluKL
Copy link
Author

leoluKL commented Oct 30, 2020

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.

@Giotino
Copy link
Collaborator

Giotino commented Oct 30, 2020

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');
})();

@Giotino
Copy link
Collaborator

Giotino commented Oct 30, 2020

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.

@leoluKL
Copy link
Author

leoluKL commented Nov 4, 2020

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.

@felixfbecker
Copy link

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.

@szmarczak
Copy link
Collaborator

@felixfbecker See #1503 - your issue is almost the same.

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