By Abobakr
I upgraded nodejs to LTS version 12.14.1, this version has a more strict SSL control which i cannot pass when attempting to make a POST request.
The POST request:
function PostReq() {
let chunks = [], resData = '';
const postData = decodeURIComponent(qs.stringify({
property1: 'value1',
property2: 'value2'
}));
const options = {
headers: {
host: 'api.example.com',
path: '/endpoint/base/resource',
port: 443,
method: 'POST',
'Content-Type': 'application/x-www-form-urlencoded;',
'Content-Length': Buffer.byteLength(postData),
}
};
const reqPost = https.request(options, (res) => {
res.setEncoding('utf8');
if (res.statusCode === 200) {
console.log('got successfull http response');
res.on('data', (chunk) => {
console.log('getting chunks...');
chunks.push(chunk);
});
res.on('end', () => {
resData = Buffer.concat(chunks).toString();
console.log('response body data: ', resData);
});
} else {
console.error('received http status code: ', res.Statuscode);
}
});
reqPost.write(postData);
reqPost.on('error', (err) => {
console.error('HubSpot API Oauth error: ', err);
});
reqPost.end();
}
PostReq();
Error message:
Error [ERR_TLS_CERT_ALTNAME_INVALID]: Hostname/IP does not match certificate's altnames: Host: api.example.com. is not in the cert's altnames: mydomain.com, DNS:www.mydomain.com
at Object.checkServerIdentity (tls.js:283:12)
at TLSSocket.onConnectSecure (_tls_wrap.js:1331:27)
at TLSSocket.emit (events.js:223:5)
at TLSSocket._finishInit (_tls_wrap.js:794:8)
at TLSWrap.ssl.onhandshakedone (_tls_wrap.js:608:12) {
reason: "Host: api.example.com. is not in the cert's altnames: mydomain.com, DNS:www.mydomain.com"
The stack:
Ive tried setting the NPM configuration setting ssl-strict to false but it didnt help. There is an solution that probably works, which is setting rejectUnauthorized value to false in the tls.connect() parameter but this causes serios security issues which im trying to avoid so id be grateful if anyone knows about a solution.
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
Ive tried with Ubuntu’s LTS version of nodejs which is < 8 and it gave me the same error, so nodejs version is irelevant. I tried doing the request in curl and it worked fine so the issue is within nodejs.
SOLVED.
I did a pretty obvious mistake in my code above, i added all the options properties in the headers object.
What i did:
const options = {
headers: {
host: 'api.example.com',
path: '/base',
port: 443,
method: 'POST',
'Content-Type': 'application/x-www-form-urlencoded;',
'Content-Length': Buffer.byteLength(postData),
}
};
What i should have done (the correct way):
const options = {
host: 'api.example.com',
path: '/base',
port: 443,
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded;',
'Content-Length': Buffer.byteLength(postData)
}
};
So if you are getting this issue, double check your code formatting, sometimes the issue is just an simple flaw!
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.
New accounts only. By submitting your email you agree to our Privacy Policy
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.