Tuesday, 16 May 2017

Looping over an array of http methods

Continuing my previous post, i faced another issue: I'm trying to insert an array (containing GET, POST, etc) as a value of the http method property, loop over it, and test the several requests. But with the array I get the 5 elements (request methods) with the 501 error. I've tried several solutions, including targeting a specific position of the array, but non of it works...Any help would apreciated. The code:

var http = require('http');


const options = {
    hostname: 'www.google.com',
    protocol: 'http:',
    method: ['GET', 'POST', 'DELETE', 'PUT', 'STARDUST']
}


var callback = function (response) {
    var exportJson = JSON.stringify(response.headers);
    var arrayData = [];
    response.on('data', function (data) {
        arrayData += data;

    });

    response.on('end', function () {
        console.log('THE DATA IS ' + arrayData);


    });

}

var x;
var req;

function test() {

    x = options.method;  
    console.log(x[0]); //This works, I can read the GET

    for (var i = 0; i < x.length; i++) {

        req = http.request(x, callback);
        //req.x[0]; The console says that doesn't know the 0 property...
        req.x;
        req.end();

    }
}

test();



via glassraven

No comments:

Post a Comment