NodeJS Request Post不适用于HTTPS网站

时间:2017-10-27 06:40:09

标签: javascript node.js loopbackjs

我正在使用NodeJS Request - Simplified HTTP Client

我似乎在使用HTTPS网站时遇到问题,我没有得到结果。

var request = require('request');

request.post({
    url: "",//your url
    headers: {
        'Content-Type': 'application/x-www-form-urlencoded'
    },

    form: {
        myfield: "myfieldvalue"
    }
},function (response, err, body){
    console.log('Body:',JSON.parse(body));
}.bind(this));

在Postman上测试了API端点(我无法分享),我只关闭SSL并且它可以工作,我如何对请求插件做同样的事情?

1 个答案:

答案 0 :(得分:3)

只需添加以下行:

    rejectUnauthorized: false,//add when working with https sites
    requestCert: false,//add when working with https sites
    agent: false,//add when working with https sites

所以你的代码看起来像这样:

var request = require('request');

request.post({
    url: "",//your url
    headers: {
        'Content-Type': 'application/x-www-form-urlencoded'
    },
    rejectUnauthorized: false,//add when working with https sites
    requestCert: false,//add when working with https sites
    agent: false,//add when working with https sites
    form: {
        myfield: "myfieldvalue"
    }
},function (response, err, body){
    console.log('Body:',JSON.parse(body));
}.bind(this));