Search code examples
javascriptfetch-api

How can I skip certificate validation with a Fetch API request within the browser?


I have the following code running in the browser:

  const response = await fetch(`${url}`, {
    method: 'POST',
    headers: {
      Authorization: `Basic ${authorization}`,
    },
    body: loginData,
  })

when I call this, I get the following response back from the server:

<URL> net::ERR_CERT_AUTHORITY_INVALID

I control the server and it is using a self-signed certificate.

In my fetch request, I want to ignore or skip the certificate validation.

I have tried:

  const response = await fetch(`${url}`, {
    method: 'POST',
    headers: {
      Authorization: `Basic ${authorization}`,
    },
    body: loginData,
    agent: false,
    rejectUnauthorized: false,
  })

but the behavior remains the same.

How can I skip the validation?


Solution

  • The browser is blocking the request, it has nothing to do with your code snippet. You need to add an exception for your cert.

    Chrome: https://superuser.com/a/41937

    Firefox: https://superuser.com/a/41937