question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Getting the response status when piping data

See original GitHub issue

Hi,

I need to download data with Node and I’m using superagent to pipe data to a file stream. The example on the documentation works fine for simple cases, but I’m now trying to handle status like 403 or 404.

I understand these are not actual errors so the response body will be piped. I should, however, receive an event that tells me the status so I can abort before I write the data. Unfortunately, the end callbacks are called without the response/error when the request is piped. The callback arguments are always undefined.

Here’s the list of handlers I’ve tried:

superagent
      .get('http://httpbin.org/image/png')
      .on('response', (response) =>
      {
          console.log('response', response);
      })
      .on('error', (error) =>
      {
          console.log('error', error);
      })
      .on('end', (error, response) =>
      {
          console.log('end', error, response);
      })
      .end(response =>
      {
          console.log('end', response);
      })
      .pipe(file);

What’s the proper way to get the response status in that case?

Thanks, Warren.

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Reactions:6
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
joewestcottcommented, Sep 8, 2016

Is this still the suggested way to handle errors when downloading/streaming to file? I get request.abort is not a function running this.

var stream = fs.createWriteStream('path/to/file')
var request = superagent
    .get('http://httpbin.org/image/png')
    .on('response', function(response) {
        if (response.status !== 200) {
            request.abort();
        }
    })
    .pipe(stream);
0reactions
kornelskicommented, Jan 31, 2018

You get the abort event, and no more. It stops downloading further data.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Getting the response status when piping data #565 - GitHub
Hi,. I need to download data with Node and I'm using superagent to pipe data to a file stream. The example on the...
Read more >
How does simply piping to the response object render data to ...
The answer is that pipe returns the destination stream. That means when you do a.pipe(b) , you'll get b back from the method...
Read more >
Get exit status of process that's piped to another
bash and zsh have an array variable that holds the exit status of each element (command) of the last pipeline executed by the...
Read more >
Pipelines API - GitLab Docs
Pipelines pagination. By default, GET requests return 20 results at a time because the API results are paginated. Read more on pagination.
Read more >
Invoke an AWS Lambda function in a pipeline in CodePipeline
CodePipeline(); // Retrieve the Job ID from the Lambda action var jobId ... statusCode; response.on('data', function (chunk) { pageObject.body += chunk; }); ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found