NodeJS请求帖子不适用于HTTPS网站

2 javascript node.js loopbackjs

我正在使用NodeJS 请求-简化的HTTP客户端

我似乎在使用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));
Run Code Online (Sandbox Code Playgroud)

在Postman上测试了API端点(我无法共享),我只是关闭了SSL并可以使用,如何使用请求插件执行相同操作?

Mar*_*osa 5

只需添加以下行:

    rejectUnauthorized: false,//add when working with https sites
    requestCert: false,//add when working with https sites
    agent: false,//add when working with https sites
Run Code Online (Sandbox Code Playgroud)

因此,您的代码如下所示:

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));
Run Code Online (Sandbox Code Playgroud)

  • 在我看来,在答案中加以澄清将是一个好主意,这样没人会在生产中使用这种方法。 (2认同)