curl获取POST响应,但node.js没有获得POST响应

当我使用下面的命令时,我得到正确的JSON响应:

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

当我使用下面的节点JS代码,我没有得到回应:

 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); } }); 

添加标题到您的请求作为这些:

 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' }, 

您将根据需要得到回应:

 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}} 

当你curl它默认添加这些标题。

每当您从客户端或库User-Agenthost Connection都应该始终添加Connection标头。 通常所有网站都需要这些标题。

要获取这些标题的值,请在浏览器中运行URL并按F12,在Net中读取由浏览器发送的请求标题数据。 在您的请求中input相同的标题。