2

I am using the following code just to get basic response from a web server with a self-signed certificate. Basic authentication is need.

I am trying the code below via Node.js (with the http module), but it fails after a few seconds. I understand that it gets no response from the server and it times out.

{[Error: socket hang up] code: 'ECONNRESET' }
Problem with request: undefined

Code:<BR>
const fs = require('fs');<BR>
const http = require('http');<BR>
const https = require('https');<BR>
var options = {<BR>
    method: "GET",<BR>
    hostname: "ServerName.com",<BR>
    port: 8445,<BR>
    rejectUnauthorized: 'false',<BR>
    cert: fs.readFileSync('Certname.cer'),<BR>
    ca: [ fs.readFileSync('Certname.cer') ],<BR>
    checkServerIdentity: () => { return null; },<BR>
    path: '/finesse/api/User/TEST/Dialogs'<BR>
    Authorization:'Basic',<BR>
    cacheControl: 'no-cache',<BR>
    acceptEncoding: "gzip, deflate",<BR>
    username: user,<BR>
    password: passw,<BR>
    Connection: 'keep-alive'<BR>
};<BR>

var req = http.request(options, function(res){<BR>
    var responseBody ="";<BR>
    console.log(`server status: ${res.statusCode}`);<BR>
    console.log("Responcer Headers: %j" , res.headers);<BR>
    res.setEnconding("UTF-8");<BR>
    res.once("data", (chunk)=>{ <BR>
        console.log(chunk);<BR>
    });<BR>
    res.on('data', function(chunk) { <BR>
        console.log(`---chunk-- ${chunk.length}`);<BR>
        responseBody +=chunk;<BR>
    });<BR>
    res.on("end", function() {<BR>
        fs.writeFile("userDialog.html", responseBody, function (error) {<BR>
            if (error) throw error; <BR>
        });<BR>
    });<BR>
});<BR>
req.on("error", function(err) {<BR>
    console.error(err);<BR>
    console.log(`Problem with request: ${err.mesage}`)<BR>
});<BR>

The same request when done via Postman returns the correct content. I even copied the request code from Postman and tried it, but with the same results. In Postman, there are also two more options in the header:

"User-Agent": "PostmanRuntime/7.13.0", "Postman-Token": ....

I don't know if I need those... Any ideas?

Thanks alot!

Anaksunaman
  • 17,239
  • Is this server http or https? http.request with no URL defaults to http, but much of the information in your Q only makes sense for https. That's also not the right way to do basic auth (but that would cause an HTTP-level error, not a connection error). – dave_thompson_085 Jun 28 '19 at 15:53
  • @KapetanFasarias I have restructured your question slightly. Please review the changes and make any additional edits as necessary (if you wish). – Anaksunaman Jun 28 '19 at 16:31
  • Thanks both "dave_thompson" and "Anaksunaman" for your responses. Also, my apologies on these "
    " all over the code. This is my first post here, and I messed it up a bit. @dave_thompson, Yes it is https. I have tried also https.request... But no success.
    – Kapetan Fasarias Jul 01 '19 at 07:11
  • @Anaksunaman, where can I find your changes to review them? – Kapetan Fasarias Jul 01 '19 at 07:12
  • @KapetanFasarias Assuming you are using a browser (not the Android app or similar), look for my user name at the bottom of the question. If you click the link next to that that says e.g. "edited X days ago", you'll see a history of changes to the question. Note you can remove the <BR> tags by editing the question yourself. They shouldn't be necessary. – Anaksunaman Jul 01 '19 at 07:30
  • @Anaksunaman, ok, I checked your modifications. Thanks! – Kapetan Fasarias Jul 01 '19 at 08:19
  • I checked the doc and it notes you must call req.end() even when there is no body (until the convenience method .get() which calls req.end() for you) -- did you? It's not in the posted code. – dave_thompson_085 Jul 02 '19 at 05:22
  • @dave_thompson_085. YES indeed, that was it! You are absolutely correct! However I did use "res.on("end", function()..." as you can see in my code. I thought, that would be adequate. Apparently no... Dave, thanks you so much. You saved me tons of hours of searching. I would never had spot that out. Thanks again! – Kapetan Fasarias Jul 02 '19 at 06:57

0 Answers0