qqryq
qqryq

Reputation: 1924

POST request with data in node.js

How could I send POST request with data (eg. some variables) to the https server and display the results to the end user?

Upvotes: 4

Views: 4710

Answers (1)

jwueller
jwueller

Reputation: 30996

Use the http.Client module. From the docs:

var http = require('http');
var google = http.createClient(80, 'www.google.com');
var request = google.request('GET', '/', {'host': 'www.google.com'});
request.end();
request.on('response', function (response) {
  console.log('STATUS: ' + response.statusCode);
  console.log('HEADERS: ' + JSON.stringify(response.headers));
  response.setEncoding('utf8');
  response.on('data', function (chunk) {
    console.log('BODY: ' + chunk);
  });
});

I am pretty sure that you can exchange GET with POST ;)

Upvotes: 4

Related Questions