Monday, 29 May 2017

async each on HTTP PUT

im trying to send multiple http put request to my server, but i only manage to send one JSON to the database, what i am missing here

var data1 =  JSON.stringify(require('./abc.json')),
    data2 =  JSON.stringify(require('./abe.json')),
    data3 =  JSON.stringify(require('./abd.json'));

var put_data = [data1,data2,data3];

async.each(post_data, function(data, callback){

    var post_options = {
        hostname: config.serviceHost,
        port    : '80',
        path    : '/API/Nag',
        method  : 'PUT',
        headers : {
            'Content-Type': 'application/json',
            'Authorization': config.secret
        }
    };

    post_req = http.request(post_options, function (res) {
        console.log('STATUS: ' + res.statusCode);
        console.log('HEADERS: ' + JSON.stringify(res.headers));
        res.setEncoding('utf8');
        res.on('data', function (chunk) {
            console.log('Response: ', chunk);
        });
        res.on('end', function () {
            callback();
        });
    });

    post_req.on('error', function(e) {
        console.log('problem with request: ' + e.message);
    });
    post_req.write(data); //posting data
    post_req.end();
}, function(err){
    console.log('All requests done!')
});

even tho it send 3 http request, all three request contains same data



via frodo

No comments:

Post a Comment