Node.js Question:

Tell me how to make an HTTP POST request in node.js?

Tweet Share WhatsApp

Answer:

This gets a lot easier if you use the request library.

var request = require('request');

request.post(
'http://www.abcsite.com/formpage',
{ form: { key: 'value' } },
function (error, response, body) {
if (!error && response.statusCode == 200) {
console.log(body)
}
}
);

Aside from providing a nice syntax it makes json requests easy, handles oauth signing (for twitter, etc.), can do multi-part forms (e.g. for uploading files) and streaming.

Download Node.js PDF Read All 15 Node.js Questions
Previous QuestionNext Question
How to extract POST data in node.js?Are you sure node.js execute system command synchronously?