0

I need to send a post request to a form that is using 'https://' many times, as my script goes through a list of a lot of different words (the list is quite large). It works great only up to some point, and then it stops and I get the error:

events.js:291
      throw er;  // Unhandled 'error' event

Error: connect ETIMEDOUT some.ip.addr.es:443
   at tcp.connectWrap.afterConnect [as oncomplete] (net.js:1145:16)

'some.ip.addr.es' is not what shows in the error, I just changed the ip it showed.

I'm guessing I get the error because either I do the post request the wrong way in a FOR ... IN loop or the loop simply just does it too fast and too many times and it makes the connection time out.

Because it works fine when it loops through a smaller list of words.

My code:

var querystring = require('querystring');
var https = require('https');
const fs = require('fs');
var lineReader = require('line-reader');

// This is so that i can type the first_item manually
const readline = require('readline').createInterface({
    input: process.stdin,
    output: process.stdout
  });
  
  // Type my first_item here and pass it to my doWork function
  readline.question('first item: ', first_parameter => {
    doWork(first_parameter);
    readline.close();
  });

async function doWork(first_parameter) {
    if (first_parameter.length == 0) {
        console.log("You didn't specify the first item...")
    } else {
        // Read each line of my text file and pass it to post_php
        lineReader.eachLine('myList.txt', function(line, last) {
            post_php(line, first_parameter)
        })
    }
}

async function post_php(x, first_parameter) {
    process.env['NODE_TLS_REJECT_UNAUTHORIZED'] = '0';
    
    // My post data (body)
    var post_data = querystring.stringify({
    'first_item': first_parameter,
    'second_item': x,
});
    // The post request options (connection, php path etc)
    var post_options = {
        host: 'some.website.com',
        port: '443',
        path: '/directory/form.php',
        method: 'POST',
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded',
            'Content-Length': Buffer.byteLength(post_data)
        }
    };
    
    // Connecting so that i can post to the form, there probably is a better way to do that, please let me know if so
    var post_req = https.request(post_options, function(res) {
        res.setEncoding('utf8');
        res.on('data', function (chunk) {

            // Tell me which word it is passing currently to the form
            console.log("Passing Parameter: " + x)
            });
        })

    post_req.write(post_data);
    post_req.end();
};

This code works good with a small list of words and throws no errors. With a bigger one, eventually I get hit with the ETIMEDOUT error and I'm assuming it's because either:

  1. The loop is doing too many requests in too quick time and the server can't handle it (if so, how can I slow the loop down?)

OR

  1. I'm not supposed to be sending https request to the php form this way and there is a better way to do that.

Please let me know if you can think of any possible fix for this. Thank you in advance.

j. cole
  • 31
  • 3
  • #2 isn't really relevant - a form is a form and web engines handle requests in pretty similar manners regardless of the framework. #1 seems more likely to be the issue. Have you tried adding in a `sleep` equivalent? See https://stackoverflow.com/questions/951021/what-is-the-javascript-version-of-sleep – h0r53 Nov 23 '20 at 16:05
  • Yes, and I tried it just now again with the sleep functions from the link you gave. I don't know if I'm putting it in a wrong place but no matter where I put it it seems like it waits just once, and then the https.request sort of acts like a whole new loop itself, processing each word from my list really fast again until the error happens. It's weird, I don't get it. That's why I was wondering if perhaps it's the issue with my code for sending the request to php. – j. cole Nov 23 '20 at 16:25
  • I would try putting the sleep call before the call to `post_php` in `lineReader.eachLine` – h0r53 Nov 23 '20 at 16:43
  • When I do that, the post_php script doesn't execute at all – j. cole Nov 23 '20 at 16:56
  • Actually it just waits once (tried another sleep function) and then the send_php runs quickly again – j. cole Nov 24 '20 at 12:58

0 Answers0