Skip to content

Instantly share code, notes, and snippets.

@ketanghumatkar
Created November 26, 2013 13:08
Show Gist options
  • Save ketanghumatkar/7658020 to your computer and use it in GitHub Desktop.
Save ketanghumatkar/7658020 to your computer and use it in GitHub Desktop.
This file describe the API Call with nodejs
var http = require('http');
data = {"foo":"bar"}
// create the JSON object
jsonObject = JSON.stringify(data);
// prepare the header
var postheaders = {
    'Content-Type' : 'application/json',
    'Content-Length' : Buffer.byteLength(jsonObject, 'utf8')
};
// the post options
var optionspost = {
    host : 'localhost',
    port : 3000,
    path : '/error_requests',
    method : 'POST',
    headers : postheaders
};
console.info('Options prepared:');
console.info(optionspost);
console.info('Do the POST call');
// do the POST call
var reqPost = http.request(optionspost, function(res) {
    console.log("statusCode: ", res.statusCode);
  console.log("headers: ", res.headers);
 
    res.on('data', function(d) {
        console.info('POST result:\n');
//         process.stdout.write(d);
console.info(d);
        console.info('\n\nPOST completed');
    });
});
 
// write the json data
reqPost.write(jsonObject);
reqPost.end();
// handle error
reqPost.on('error', function(e) {
    console.error(e);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment