Home > Enterprise >  NodeJS ending https response early
NodeJS ending https response early

Time:11-15

I have a nodejs application that sends http requests to an external API and retrieves json data (in the form of a hex buffer) in response, using http.

However, when I try to pull large data sets from the API, the data is incomplete. The json will cut off about a third of the way through and trying to parse it produces an error. I don't think it's a problem with my parsing (toString) because res.complete is not being triggered, so it's clearly not finishing.

Is there a way to force my request to wait for res.complete to finish?

My request looks like this:

const req = https.get(options=pull_options,res=> {
   res.on('data', d=> {
      if(res.complete) {
         resolve(d.toString());
      } else {
         console.error("Connection terminated while message was still being sent.")
      }
   }
}

I really don't think it's a problem with the API cutting me off because I'm able to pull the same data set with nearly identical code in python with no issues.

CodePudding user response:

output = '';
const req = https.get(options=pull_options,res=> {
   res.on('data', d=> {
      output  = d;
   }
   res.on('end', d=> { 
      resolve(output)
   }
}

Changing the resolve to on end to allow all the data to come in worked for me.

  • Related