Showing posts with label NodeJS-request. Show all posts
Showing posts with label NodeJS-request. Show all posts

Thursday 15 June 2017

request module with nodeJs tutorial

How to post get/post request with parameter? How to send headers in ajax call.

Question: What is request module?
Request is designed to be the simplest way possible to make http/https calls.



Question: How to install request module?
npm install request




Question: How to include request module in node project?
var request = require('request');




Question: Give example of GET http call?
var request = require('request');
request('http://domain.com/ajax.php', function (err, response, body) {
   if (err) {
    return console.error('upload failed:', err);
  }
    console.log(body);  
});




Question: Give example of POST http call with parameter?
request({method:'post',url:'http://domain.com/ajax.php', form: {name:'hello',age:25},headers:options}, function (error, response, body) {  
    console.log(body);  
});




Question: How to send custom header with post method?
var headersOpt = {  
    'User-Agent': 'request'  
};
request(
        {
        method:'post',
        url:'http://domain.com/ajax.php', 
        form: {name:'hello',age:25}, 
        headers:headersOpt
    }, function (error, response, body) {  
        
        console.log(body);  
});




Question: How to set cookie with request?
request.cookie('key1=value1')