我正在创建一个连接到Proxmox 2.1的REST api的NodeJS服务。

我的剧本:

// module dependencies
var http    = require('http'),
    url     = require('url'),
    https   = require('https'),
    querystring = require('querystring'),
    fs     = require('fs');

exports.urlReq = function(reqUrl, options, cb){
    if(typeof options === "function"){ cb = options; options = {}; }// incase no options passed in

    // parse url to chunks
    reqUrl = url.parse(reqUrl);

    // http.request settings
    var settings = {
        host: reqUrl.hostname,
        port: reqUrl.port || 80,
        path: reqUrl.pathname,
        headers: options.headers || {},
        method: options.method || 'GET',
        requestCert: false
    };

    // if there are params:
    if(options.params){
        options.params = querystring.stringify(options.params);
        settings.headers['Content-Length'] = options.params.length;
    };


    // MAKE THE REQUEST
    var req = https.request(settings);

    req.on('error', function(err) {
        console.log(err);
    });

    // when the response comes back
    req.on('response', function(res){
        res.body = '';

        console.log("statusCode: ", res.statusCode);
        console.log("headers: ", res.headers);

        res.setEncoding('utf-8');

        // concat chunks
        res.on('data', function(chunk){ res.body += chunk });

        res.on('error', function(err){
            throw err;
        })

        // when the response has finished
        res.on('end', function(){

            // fire callback
            cb(res.body, res);
        });
    });

    // if there are params: write them to the request
    if(options.params){ req.write(options.params) };

    // end the request
    req.end();
}


该脚本适用于GET请求,尽管在执行POST请求时会死掉。它不会引发任何错误,只是默默地失败。

当控制台记录响应时,这是res.connection对象:

connection:
      { pair: [Object],
        writable: true,
        readable: true,
        _paused: false,
        _needDrain: false,
        _pending: [],
        _pendingCallbacks: [],
        _pendingBytes: 0,
        socket: [Object],
        encrypted: [Object],
        authorized: false,
        _controlReleased: true,
        _events: [Object],
        _pool: <Buffer 48 54 54 50 2f 31 2e 31 20 34 30 30 20 50 61 72 61 6d 65 74 65 72 20 76 65 72 69 66 69 63 61 74 69 6f 6e 20 66 61 69 6c 65 64 20 2d 20 64 75 70 6c 69 63 ...>,
        _poolStart: 332,
        _poolEnd: 65536,
        parser: [Object],
        _httpMessage: [Circular],
        ondata: [Function: socketOnData],
        onend: [Function: socketOnEnd],
        npnProtocol: undefined,
        authorizationError: 'UNABLE_TO_VERIFY_LEAF_SIGNATURE' },


服务器使用自签名的SSL证书。

任何帮助将不胜感激,

谢谢!

最佳答案

这已经很老了,但是最近我与Proxmox遇到了类似的问题,并希望在其他人看到此问题的情况下做出贡献。

要解决“ authorizationError:'UNABLE_TO_VERIFY_LEAF_SIGNATURE'”错误,您可以指示node.js接受自签名证书(默认情况下拒绝该证书),请将其放在脚本顶部:

process.env ['NODE_TLS_REJECT_UNAUTHORIZED'] ='0';

另外,我无法在您的代码中告诉您是否设置了Content-Type标头,但对于POST到Proxmox API的情况,必须将其设置为application / x-www-form-urlencoded。也许您已经在options.header中设置了它,但是当您指定Content-Length时,我不确定。 POST也需要CSRFPreventionToken,因此也应将其作为标头的一部分传递。

10-06 09:34