I'm making a script that hits an API (that I can't disclose unfortunately).
It allows me to submit a url, path, port, and a couple of boolean fields.

My script first takes in a list of URLs (where each url is on a new line) and puts them in a array. Each url has its own index. 

Then I have a function that creates and posts a http request.
Here is what is sent in the request:
`{ "url": url, "path": "/", "port": "443", "live_scan": "false", "advanced": "true" }`

where `url = array[counter]`

From what I have been able to see thru various console.logs, everything up to the actual write function works.

At that point, i believe that the JSON is never being sent as my response is always `undefined` or more accurately, `{"response":{}}`

Here is my full code:

```
var http = require('http');
var fs = require('fs');
var EventEmitter = require('events').EventEmitter;

// An object of options to indicate where to post to

var post_options = {
    host: 'localhost',
    port: '8080',
    path: '/api/scan',
    method: 'POST'
};

fs.readFile('urls.txt', function(err, data) {

    //if (err) throw err;
    //console.log(err);
    var array = data.toString().split('\n');
    for (var i = 0; i < array.length - 1; i++) {
        str = array[i];
        array[i] = str.slice(0, -1);
    }
    //console.log(array);

    var emitter = new EventEmitter();
    var counter = 1,
        n = array.length;
    //console.log(n);

    // Start with the first request

    function PostRequest() {
        var post_req = http.request(post_options, function(res) {
            res.setEncoding('utf8');
            var body = '';
            res.on('data', function(chunk) {
                body += chunk;
            });
            res.on('end', function() {
                //body = JSON.parse(body);
                // Make sure it's working
                console.log(body);

                // ADD THE CALLBACK 
                // OR 
                // TRIGGER EVENT
                //PostRequest();
                return emitter.emit('ResponseEnded');

            });
        });

        var url = array[counter];
        //console.log(url);
        var catURL = { "url": url, "path": "/", "port": "443", "live_scan": "false", "advanced": "true" };
        console.log(catURL);
        var jsonURL = JSON.stringify(catURL);

        post_req.write(jsonURL, function(err) {
            if (err) throw err;
            //console.log(err);

        });
        post_req.end();
    }

    emitter.on('ResponseEnded', function() {
        counter++;
        if (counter < n) {
            PostRequest();
        } else {
            console.log('No more requests');
        }
    });

    PostRequest();
});

```

Am I making a stupid newbie mistake? Or is this just the completely wrong method to making a request?

Thanks for all/any help!

Oh, I have to say, I have a way of doing this where i manually type each url into the array, and never use a filestream, and it works perfectly that way, so am I using fs incorrectly?

Here is the code for the hardcoded URL array:

```
var http = require('http');
var fs = require('fs');
var EventEmitter = require('events').EventEmitter;

// An object of options to indicate where to post to

var post_options = {
    host: 'localhost',
    port: '8080',
    path: '/api/scan',
    method: 'POST',
    headers: {
        'Content-Type': 'application/json'
    }
};

var array = ['ssl.com', 'google.com', 'hamzakhan.org'];

var emitter = new EventEmitter();
var counter = 0,
    n = array.length;
console.log(n);

function PostRequest() {
    var post_req = http.request(post_options, function(res) {
        res.setEncoding('utf8');
        var body = '';
        res.on('data', function(chunk) {
            body += chunk;
        });
        res.on('end', function() {
            body = JSON.parse(body);
            // Make sure it's working
            console.log(body.response.subject);

            // ADD THE CALLBACK 
            // OR 
            // TRIGGER EVENT
            //PostRequest();
            return emitter.emit('ResponseEnded');

        });
    });

    var url = array[counter];
    var catURL = { "url": url, "path": "/", "port": "443", "live_scan": "false", "advanced": "true" };
    post_req.write(JSON.stringify(catURL), function(err) {
        //console.log(err);
        post_req.end();
    });
}

emitter.on('ResponseEnded', function() {
    ++counter;
    if (counter < n) {
        PostRequest();
    } else {
        console.log('No more requests');
    }
});

// Start with the first request
PostRequest();

```