【问题标题】:Always getting "Socket Hang Up with 'ECONNRESET'"总是收到“使用 'ECONNRESET' 挂断套接字”
【发布时间】:2018-10-02 20:11:42
【问题描述】:

当尝试在回调方法的帮助下总是出错但总是出错并尝试在 NodeJS 的 POST HTTP 方法中调用 API 时,尝试了所有解决方案但一无所获。

exports.createWallet = function(user_id, password, callback) {

var api_code = config.blockchain.api_code;
var user = user_id;
var pwd = password;

var result;

const post_data = JSON.stringify({
    'api_code': api_code,
    'password': pwd,
    'user_id': user
});

const options = {

    hostname: 'localhost',
    port: 8586,
    path: '/api/v2/create',
    method: 'POST',
    headers: {
        'Content-Type': 'application/x-www-form-urlencoded',
        'Content-Length': Buffer.byteLength(post_data)
    }
};

var request = https.request(options, function (response) {
});

request.on('error', function (error) {

    console.log(error);
});

// With http.request() one must always call request.end() to signify the end of the request - even if there is no data being written to the request body.
request.end();
};
{ Error: socket hang up

    at TLSSocket.onHangUp (_tls_wrap.js:1120:19)
    at Object.onceWrapper (events.js:293:19)
    at emitNone (events.js:91:20)
    at TLSSocket.emit (events.js:188:7)
    at endReadableNT (_stream_readable.js:975:12)
    at _combinedTickCallback (internal/process/next_tick.js:80:11)
    at process._tickCallback (internal/process/next_tick.js:104:9) code: 'ECONNR
ESET' }

【问题讨论】:

    标签: javascript node.js sockets post econnreset


    【解决方案1】:

    在选项中,尝试添加body: post_data

    【讨论】:

    • const options = { 主机名:'localhost',端口:8586,路径:'/api/v2/create?api_code'+api_code+'&password'+pwd+'&user_id'+user,方法:' POST',协议:'https:',rejectUnauthorized:true,strictSSL:false,正文:post_data,标头:{'Content-Type':'application/x-www-form-urlencoded','Content-Length':缓冲区.byteLength(post_data) } };
    • 添加正文:post_data,在选项中但再次出现相同的错误
    • 并尝试其他选项,但得到相同的“Socket hang up with ECONNRESET”
    • 还有一点,'Content-Type': 'application/json'。除此之外,在您的应用程序中,确保您的应用程序是否需要任何 access_token。如果是这样,它是否期望它作为标头或请求 url。相应地执行此操作。而且,不要在请求 url 中传递您的 post_data。这是在正文中传递的。
    • const options = { 主机名:'localhost',端口:8586,路径:'/api/v2/create?api_code='+api_code+'&password='+pwd+'&user_id='+user,方法:'POST',}; var request = http.request(options, function (response) { }
    【解决方案2】:

    //只使用不带https & 在路径中传递参数

    var http = require("http");     
    const options = {
    
        hostname: 'localhost',
        port: 8586,
        path: '/api/v2/create?api_code='+api_code+'&password='+pwd+'&user_id='+user,
        method: 'POST',
    };
    
    var request = http.request(options, function (response) {
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-06-26
      • 2015-11-07
      • 2015-08-27
      • 2015-09-19
      • 1970-01-01
      • 2021-03-20
      • 2018-02-04
      • 1970-01-01
      相关资源
      最近更新 更多