How to use request module in node js

16-03-2017 request module node js

The request module is similar to http module. The syntax for installing this module is

npm install request --save

Include this module by using below syntax.

var request = require('request');

the syntax for request 

try{
	var headers = {
		'User-Agent':       'Super Agent/0.0.1',
		'Content-Type':     'application/x-www-form-urlencoded'
	}
	var options = {
		url: 'URL',
		method: 'POST',
		headers : headers,
		form: {'KEY1': 'VALUE1', 'KEY2' : 'VALUE1'}
	};
	request(options, function (error, response, body) {
		console.log('   response.statusCode  '+response.statusCode);
		if (!error && response.statusCode == 200) {
			// Print out the response body
			var body = JSON.parse(body);	
			console.log(body);
		}else{
			console.log('   error   '+ error);
		}
	});
}
catch(e){
	console.log(e);
}

 

You might also like:

Detect AdBlock with JavaScript in my browser
30-07-2017 AdBlock javascript detect
How to redirect one page to another page in php
20-03-2017 redirect php
How to use http-post request in node js
14-03-2017 http-post module node js
Sanitization in java sript
14-03-2017 sanitization java script
Sample Validations in javascript
22-02-2017 validations javascript