amolgautam
amolgautam

Reputation: 967

Curl is getting POST response but node.js is not getting POST response

When i use the following command i get correct JSON response:

$ curl --data "regno=<reg-number>&dob=<dob>&mobile=<mobile>"  https://vitacademics-rel.herokuapp.com/api/v2/vellore/login

When i use the following Node JS code i dont get response:

var request= require('request');
var querystring=require('querystring');

var credentials={regno: '13bit0036', dob:25051995, mobile:9431222422};
console.log(querystring.stringify(credentials));
request.post({
url: 'https://vitacademics-rel.herokuapp.com/api/v2/vellore/login',

headers: {'Content-Type': 'application/x-www-form-urlencoded'},
body:querystring.stringify(credentials),
}, function(error, response, body){
if(error) {
    console.log(error);
} else {
    console.log(response.statusCode + '\n' , body);
}
});

Upvotes: 1

Views: 1232

Answers (1)

Lalit Umbarkar
Lalit Umbarkar

Reputation: 443

Add headers to your request as these:

headers: {
    'User-Agent':'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:31.0) Gecko/20100101 Firefox/31.0',
    'host': 'vitacademics-rel.herokuapp.com',
    'Content-Type': 'application/x-www-form-urlencoded',
    'Connection':'keep-alive'
},

You will get response as required:

lalit@ubuntu-0:~$ node requesting.js 
regno=13bit0036&dob=25051995&mobile=9431222422
200
{"reg_no":"13BIT0036","dob":"25051995","mobile":"9431222422","campus":"vellore","status":{"message":"Successful execution","code":0}}

When you do curl it adds these headers by default.

Whenever you do a connection from a client or library User-Agent, host, Connection headers should always be added. Usually all websites require these headers.

To get the values for these headers, run URL in your browser and press F12, in Net read the request headers data send by browser. Enter same headers in your request.

Upvotes: 2

Related Questions