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:
- 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
- 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.