Skip to content

Instantly share code, notes, and snippets.

@shinaisan
Created October 29, 2016 06:29
Show Gist options
  • Save shinaisan/eb642bf97c853575d0fd03f3cd9effb1 to your computer and use it in GitHub Desktop.
Save shinaisan/eb642bf97c853575d0fd03f3cd9effb1 to your computer and use it in GitHub Desktop.
HTTP GET samples with node.js.

HTTP GET samples with node.js ...

  • using http.get.
  • using http.request.
  • using request.get.
var http = require('http');
var querystring = require('querystring');
var params = {
name: "foo"
};
var q = querystring.stringify(params);
var options = {
host: 'localhost',
port: 10010,
path: '/hello?' + q
};
var req = http.get(options, function(res) {
var buffer = null;
if (res.statusCode != 200) {
console.log(res.statusCode, res.statusMessage);
return;
}
res.on('data', function(chunk) {
if (buffer) {
buffer = Buffer.concat([buffer, chunk]);
} else {
buffer = chunk;
}
});
res.on('end', function() {
console.log(buffer.toString());
});
}).on("error", function(e) {
console.log(e);
});
var http = require('http');
var querystring = require('querystring');
var params = {
name: "foo"
};
var q = querystring.stringify(params);
var options = {
host: 'localhost',
port: 10010,
method: 'GET',
path: '/hello?' + q
};
var req = http.request(options, function(res) {
var buffer = null;
if (res.statusCode != 200) {
console.log(res.statusCode, res.statusMessage);
return;
}
res.on('data', function(chunk) {
if (buffer) {
buffer = Buffer.concat([buffer, chunk]);
} else {
buffer = chunk;
}
});
res.on('end', function() {
console.log(buffer.toString());
});
}).on("error", function(e) {
console.log(e);
});
req.end();
var request = require('request');
var querystring = require('querystring');
var params = {
name: ""
};
var q = querystring.stringify(params);
var url = 'http://localhost:10010/hello?' + q;
request.get(url, function (error, response, body) {
if (!error && response.statusCode == 200) {
console.log(body);
} else {
console.log(response.statusCode, response.statusMessage);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment