I have a custom monitor code setup. I need to check if the specified URL has its SSL certificate expired. Is there a specific HTTP code for this? For example:
(async() => {
const res = await fetch('http://example.com');
// Or `statusText`
if (res.status == some_ssl_expiryCode) {
console.log('SSL expired!');
}
})();
Is there a way to do this in javascript?
Thank you.
The client application receives an HTTP 400 - Bad request response with the message "The SSL certificate error".
As @Jasen said, the connection doesn't reach the server. The fetch request returns the error CERT_HAS_EXPIRED or CERT_INVALID. It does not produce an HTTP code. See the javascript code below.
import fetch from 'node-fetch';
fetch('https://example.com/')
.then(async(res) => await res.text())
.then(() => { // Do something if success })
.catch(error => {
console.error(error);
if (error.code === 'CERT_HAS_EXPIRED') {
// Certificate has expired
}
if (error.code === 'CERT_INVALID') {
// Certificate is invalid
}
});
Hope this helps someone else who comes across this thread!