Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

241
Views
How do you restart a streaming HTTP request on an error

I am accessing an API that streams (RFC2616 for HTTP/1.1) the response. The stream never ends, but from time to time it throughs an error requiring the program to restart the request. I can't figure out how in NodeJS to restart the request without restarting the program. I've tried loops to check if an error occurred, but I end up creating a blocking code that stops the request methods. The code below works until an error occurs.

const request = require('request');

const options = {
  method: 'GET',
  url: 'https://api.tradestation.com/v3/marketdata/stream/quotes/IWM',
  headers: {Authorization: 'Bearer ' + api.token,},
};

request(options)
  .on('data', function (data) {
    console.log(data.toString());
    //Process data here
  })
  .on('error', function (err) {
    console.error(err);
  });

If it helps, the documentation to the API for streaming is here, and the API call is here.

On a side note, I tried using Axios but it doesn't seem to like endless streams (tried onDownloadProgress).

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

I would stick your request inside a function, you can then call it again on error.. I would also put a slight delay on the retry..

eg.

function callAPI() {
  request(options)
    .on('data', function (data) {
      console.log(data.toString());
      //Process data here
    })
    .on('error', function (err) {
      console.error(err);
      setTimeout(callAPI, 1000); //wait a second and retry.
    });
}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!