How to make an http request in node js

http
module
node js

In node js there is an node module like http. Now include the http module into server file. By using this node module we can call webservices also.

var http = require('http');

Syntax for make http request.

try{
		var Authorization = "Basic " + new Buffer("username:password").toString("base64");
		var options = {
			host: 'IP_ADDRESS',
			port: PORT_NUMBER,
			method: 'GET',
			headers : {
				"Authorization" : Authorization
			}
		};
		
		var reqp = http.request(options, function(resp) {
			resp.setEncoding('utf8');
			resp.on('data', function (chunk) {
				// the response is in string format. So we need to convert to json.
				var resdata = JSON.parse(chunk);
				console.log(resdata);
			});
		});

		req.on('error', function(e) {
		  console.log('problem with request: ' + e.message);
		});

		// write data to request body
		reqp.write('data\n');
		reqp.write('data\n');
		reqp.end();			
}catch(e){
	console.log(e);
}

 

You might also like:

Use java script file client side and server side at a time in node js

30-07-2017 node js include call javascript

How to submit form using javascript or jquery.

16-04-2017 javascript jquery submit form

Change datepicker display format

06-03-2017 datepicker angular js

Filter for change Date Time format (MM-dd-yyyy HH:mm) in Angular Js

05-03-2017 mm-dd-yyyy HH:MM date formats angular js

Filter for replace text in Angular Js

05-03-2017 filter replace angular js