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!
http.requestwith 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" 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
<BR>tags by editing the question yourself. They shouldn't be necessary. – Anaksunaman Jul 01 '19 at 07:30req.end()even when there is no body (until the convenience method.get()which callsreq.end()for you) -- did you? It's not in the posted code. – dave_thompson_085 Jul 02 '19 at 05:22