我有nodeApp。它确实有用。
在特定时间,我需要与野外的API进行通信。在像Postman这样的休息工具中使用API是直截了当的:
Url:
https://epicurl
Headers:
Content-Type : application/json
Accept : application/json
x-key : secret
Body:
{
"some":"kickass"
"data":"here"
}
在Postman中发送以上内容我得到了一个很好的快速回复!是的休息工具。
所以他们的API工作,现在我需要在我的Node.js应用程序中做出相同的响应。
这就是事情变得奇怪......
var request = require('request')
...lots_of_other_stuff...
var options = {
uri: 'https://epicURL',
method: 'POST',
json: true,
headers : {
"Content-Type":"application/json",
"Accept":"application/json",
"x-key":"secretbro"
},
body : JSON.stringify(bodyModel)
};
request(options, function(error, response, body) {
if (!error) {
console.log('Body is:');
console.log(body);
} else {
console.log('Error is:');
logger.info(error);
}
cb(body); //Callback sends request back...
});
上面的失败......它引发了我们都喜欢的好的ECONNRESET错误!为什么?谁知道?
var https = require("https");
https.globalAgent.options.secureProtocol = 'SSLv3_method';
var headers = {
"Content-Type":"application/json",
"Accept":"application/json",
"x-key":"nicetrybro"
}
var options = {
host: 'www.l33turls.com',
port:443,
path: "/sweetpathsofjebus",
method: 'POST',
headers: headers
};
var req = https.request(options, function(res) {
res.setEncoding('utf-8');
var responseString = '';
res.on('data', function(data) {
responseString += data;
});
res.on('end', function() {
var resultObject = responseString;
//Call the callback function to get this response object back to the router.
cb(resultObject);
});
});
req.on('error', function(e) {
console.log(e);
});
req.write(bodyString);
req.end();
如果我在使用请求模块时保留这行代码,那么它就可以工作......
var https = require("https");
https.globalAgent.options.secureProtocol = 'SSLv3_method';
这是在某处记录的吗?我错过了吗?有人向我解释过这个吗?