Alosyius
Alosyius

Reputation: 9111

Node.js send post request with data?

How can i send the following request in a Node.js environment?

curl -s -v -X POST 'http://localhost/pub?id=my_channel_1' -d 'Hello World!'

Im trying to build a Node.js server together with the Nginx Push Stream Module.

Upvotes: 3

Views: 1857

Answers (2)

Plato
Plato

Reputation: 11052

to expand on @Silviu Burcea 's recommendation of the request module:

//Set up http server:
function handler(req,res){ 
  console.log(req.method+'@ '+req.url);
  res.writeHead(200); res.end();
};
require('http').createServer(handler).listen(3333);

// Send post request to http server
// curl -s -v -X POST 'http://localhost/pub?id=my_channel_1' -d 'Hello World!'
// npm install request (https://github.com/mikeal/request)
var request = require('request'); 
request(
{ uri:'http://localhost:3333/pub?id=my_channel_1',
  method:'POST',
  body:'Hello World!',
},
function (error, response, body) {
  if (!error && response.statusCode == 200) { console.log('Success') }
});

Upvotes: 1

Silviu Burcea
Silviu Burcea

Reputation: 5348

You may want to use the 'request' module, I am using it and I feel very comfortable with it.

Upvotes: 2

Related Questions