Search code examples
javascripthttpsslssl-certificatefetch-api

What HTTP code do you get if an SSL has expired?


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?


Solution

  • 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.

    fetch('https://example.com/')
        .then(res => res.text())
        .then(() => { /* Do something if success */ })
        .catch(error => {
            console.error(error);
            if (error.code === 'CERT_HAS_EXPIRED') {
                // Certificate has expired
            } else if (error.code === 'CERT_INVALID') {
                // Certificate is invalid
            }
        });