Curl 正在获得 POST 响应,但 node.js 未获得 POST 响应
Curl is getting POST response but node.js is not getting POST response
当我使用以下命令时,我得到正确的 JSON 响应:
$ curl --data "regno=<reg-number>&dob=<dob>&mobile=<mobile>" https://vitacademics-rel.herokuapp.com/api/v2/vellore/login
当我使用以下 Node 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 添加到您的请求中,如下所示:
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 时,它会默认添加这些 headers。
无论何时从客户端或库进行连接,都应始终添加 User-Agent
、host
、Connection
headers。通常所有网站都需要这些 headers.
要在浏览器中获取这些 headers、运行 URL 的值并按 F12,在 Net 中读取浏览器发送的请求 headers 数据。在您的请求中输入相同的 headers。
当我使用以下命令时,我得到正确的 JSON 响应:
$ curl --data "regno=<reg-number>&dob=<dob>&mobile=<mobile>" https://vitacademics-rel.herokuapp.com/api/v2/vellore/login
当我使用以下 Node 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 添加到您的请求中,如下所示:
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 时,它会默认添加这些 headers。
无论何时从客户端或库进行连接,都应始终添加 User-Agent
、host
、Connection
headers。通常所有网站都需要这些 headers.
要在浏览器中获取这些 headers、运行 URL 的值并按 F12,在 Net 中读取浏览器发送的请求 headers 数据。在您的请求中输入相同的 headers。