Created
May 12, 2014 13:47
-
-
Save Unitech/cb39287b9f083f156bd2 to your computer and use it in GitHub Desktop.
Simple POST nodejs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
obj.post = function(url, data, cb) { | |
var http = require('http'); | |
var dt = JSON.stringify(data); | |
var options = { | |
host: url, | |
path: '/new', | |
port: 3000, | |
method: 'POST', | |
headers : { | |
'Content-Length': dt.length, | |
'Content-Type': 'application/json' | |
} | |
}; | |
var req = http.request(options, function(res) { | |
res.setEncoding('utf8'); | |
res.on('data', function (chunk) { | |
console.log('BODY: ' + chunk); | |
}); | |
}); | |
req.on('error', function(e) { | |
console.log('problem with request: ' + e.message); | |
}); | |
// write data to request body | |
req.write(dt, 'utf8'); | |
req.end(); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment