K
K
kuzubina2019-08-07 11:56:19
JavaScript
kuzubina, 2019-08-07 11:56:19

What is the correct way to make an http request in a request in node.js?

Hi all!
Tell me please, am I doing the http request correctly?
In the first request, there are event ids, and in the second request, by event id, I already get detailed information and I need to output a number of information from the first and second requests to the console. Here is an example of my code

Request.get({
      url: `https://example.com/?action=get_events&APIkey`,
      json: true,
      headers: {'User-Agent': 'request'}
    }, (err, res, body) => {
      if (err) {
        console.log('Error:', err);
      } else if (res.statusCode !== 200) {
        console.log('Status:', res.statusCode);
      } else {

        if (body && body.length > 0) {

            for (let event of body) {

              let eventId = event.event_id

              Request.get({
                url: `https://example.com/?action=get_events&${eventId}&APIkey`,
                  json: true,
                  headers: {'User-Agent': 'request'}
                }, (err, res, odds) => {
                  if (err) {
                    console.log('Error:', err);
                  } else if (res.statusCode !== 200) {
                    console.log('Status:', res.statusCode);
                  } else {
                    
                    if (odds && odds.length > 0) {

                      console.log(event.event_name);

                    }

                  }
              });
            }
        }
      }
  });

but there is a problem with this solution, when there are few events in the list, they are retrieved normally, if there are more of them, then some of them do not extract information, but show such an error, and a small part is retrieved normally
Error: { Error: read ECONNRESET
    at _errnoException (util.js:1022:11)
    at TLSWrap.onread (net.js:628:25) code: 'ECONNRESET', errno: 'ECONNRESET', syscall: 'read' }

What could be the problem?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question