How to use request module in node js

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:

Simple Array Sum | Javascript | hackerrank.com

07-05-2020 hackerrank.com javascript arrays array sum

File Upload Using Angular Js and Node Js

28-05-2018 AngularJs Node js file upload multer module

How to submit form using javascript or jquery.

16-04-2017 javascript jquery submit form

Example for Conditional Operator in php

16-04-2017 php Conditional Operator example

Get random strings from given string In javascript

21-02-2017 javascript random string