简体   繁体   中英

Socket hang up on node js https request

i've searched through several tutorials and questions on stackoverflow and other sites but i can't still figure out why my script is producing an Error: socket hang up...

Hope you guys can help me I'v implemented a https server like in the tutorial on https://www.pixelstech.net/article/1445603357-A-HTTPS-client-and-HTTPS-server-demo-in-Java

works perfectly and also the client works. But when i want to create a request in javascript and run it with node js i'm getting the known error...

My .js file:

var https = require('https');

var data = JSON.stringify({
  firstName: 'JoaquÌn',
});

function getCall() {
    //initialize options values, the value of the method can be changed to POST to make https post calls
    var options = {
        host :  'localhost',
        port : 9999,
        path : '/',
        rejectUnauthorized: false,
        method : 'POST',
        headers: {'Connection': 'keep-alive',
          'Content-Type': 'application/json; charset=utf-8',
                  'Content-Length': Buffer.byteLength(data)}
    }

    //making the https get call
    var getReq = https.request(options, function(res) {
        console.log("\nstatus code: ", res.statusCode);
        res.on('data', function(data) {
            console.log( JSON.parse(data) );
        });
    });

    //end the request
    getReq.end();
    getReq.on('error', function(err){
        console.log("Error: ", err);
    }); 
}

getCall();

My Error:

Error:  { Error: socket hang up
    at createHangUpError (_http_client.js:253:15)
    at TLSSocket.socketOnEnd (_http_client.js:345:23)
    at emitNone (events.js:91:20)
    at TLSSocket.emit (events.js:185:7)
    at endReadableNT (_stream_readable.js:974:12)
    at _combinedTickCallback (internal/process/next_tick.js:80:11)
    at process._tickCallback (internal/process/next_tick.js:104:9) code: 'ECONNRESET' }

IntelliJ produces this part when i'm running the script:

SSLSession :
    Protocol : TLSv1.2
    Cipher suite : TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256
Inut : POST / HTTP/1.1
Inut : Connection: keep-alive
Inut : Content-Type: application/json; charset=utf-8
Inut : Content-Length: 24
Inut : Host: localhost:9999
Inut : 

I hope you can help me because i don't know why i'm getting the error in i've tried several solutions but none of them has worked for me...

Thx and best wishes Martin

Assuming that you already have a HTTP server running on configured port, you will need to change host: 'localhost' to host: 'http://localhost'

It could either be http or https based on the protocol you have setup.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM