0

I have this code:

var fs = require('fs');
var readline = require('readline');
var request = require("request");

var filename = process.argv[2];

readline.createInterface({
    input: fs.createReadStream(filename),
    terminal: false

}).on('line', function(line) {
sep = line.split("|");
id = sep[0];
name = sep[1];

var options = { method: 'POST',
  url: 'https://api.com/mobile_authentications',
  proxy: '...',
  headers: 
   { Host: 'api.com',
     'Content-Type': 'application/json',
     'Accept-Language': 'pt-BR',
     'X-Client-Info': 'Timezone%3D-Infinity%26device_id%3D578e4ac016b69248%26utc_offset%3D-0300%26app_id%3D7092%26resolution%3D720x1280%26connectivity%3DWIFI%26site_id%3DMLB%26density%3D320%26user_id%3Dguest' },
  body: '{"authentication_request":{"client_id":"'+id+'|'+name+'"}}' };

request(options, function (error, response, body) {
  if (error) throw new Error(error);
    if(body.includes('ATTEMPS'))
    {
        console.log("CAPTCHA");
        }
  //console.log(body);
});


});

First question: How can I check if body response contains "ATTEMPS" string?

Another problem: When the ids.txt file has more than 100 lines the script returns me this error:

  if (error) throw new Error(error);
             ^

Error: Error: tunneling socket could not be established, statusCode=502
    at Request._callback (C:\Users\PC\Desktop\top\convert.js:65:20)
    at self.callback (C:\Users\PC\Desktop\top\node_modules\request\request.js:185:22)
    at Request.emit (events.js:182:13)
    at Request.onRequestError (C:\Users\PC\Desktop\top\node_modules\request\request.js:881:8)
    at ClientRequest.emit (events.js:182:13)
    at ClientRequest.onConnect (C:\Users\PC\Desktop\top\node_modules\tunnel-agent\index.js:168:23)
    at Object.onceWrapper (events.js:273:13)
    at ClientRequest.emit (events.js:182:13)
    at Socket.socketOnData (_http_client.js:475:11)
    at Socket.emit (events.js:182:13)

That is, the script is not able to work with large files? Thanks.

  • Could it be that you send too many requests in a small amount of time? Maybe it is worth to switch to [Promises & Async/Await request API](https://github.com/request/request#promises--asyncawait) and add some timeouts or parallel connection limits? – vsemozhebuty Mar 03 '19 at 13:43
  • As for "check if body response contains "ATTEMPS" string", your approach should work (`body` is a string by fefault, right?). – vsemozhebuty Mar 03 '19 at 13:44

0 Answers0