How to use http-post request in node js

http-post
module
node js

Basically the http module is used to call webservice, but http-post is used for sending data. So install http-post module. The syntax for http-post module is 

npm install http-post

Include this module by using below syntax.

http.post = require('http-post'); 

the syntax for http-post 

try{
	var authorization = "Basic " + new Buffer("USERNAME:PASSWORD").toString("base64");
	var datas ={"KEY1":'VALUE1',"KEY2":'VALUE2'};
	var options = {
		host: 'IP_ADDRESS',
		port: 'PORT_NUMBER',
		path: 'FILE_PATH',
		method: 'POST',
		headers : {
			"Authorization" : authorization
		}
	};

	var reqp = http.post(options,datas, function(resp) {
		resp.setEncoding('utf8');
		resp.on('data', function (chunk) {
			try{
				var resdata = JSON.parse(chunk);
				res.json(resdata);
			}catch(e){
				console.log(e);
				var resdata = JSON.parse('[{"message":"Data not Found"}]');
				res.json(resdata);	
			}
		});
	});
	reqp.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:

Functions in javascript

25-05-2018 functions javascript es5 es6 arrow functions named functions Closures Nested Functions Callbacks Arrow functions.

How to find Angular Errors

06-03-2017 errors angularjs exceptionHandler

Filter for replace text in Angular Js

05-03-2017 filter replace angular js

How to set or update or push the values into Object in javacript

27-02-2017 javascript set update push object

Get Unique values from array/object in javascript

19-02-2017 javascript unique remove repeated values from array