3

How can I fetch data from a site which gives NET::ERR_CERT_AUTHORITY_INVALID using fetch API. I used this but got net::ERR_INSECURE_RESPONSE Below is my code

let header = new Headers({
  'Access-Control-Allow-Origin':'',
  'Content-Type': 'multipart/form-data'
  });
let username="***";
let password="***";
let url ="***";
let sentData={
    method:'POST',
    mode: 'cors',
    header: header
};
return new Promise((reslove,reject)=>{
    fetch(url, sentData)
        .then(response=> response.json())
        .then(responseText=>{

            console.log(responseText);

        }).catch(err=>{
        console.log(err);
    });
}).catch(err => {
        console.log(err);
    });

I can't share the url ,username and pwd.

MinnuKaAnae
  • 1,646
  • 3
  • 23
  • 35
ANISHA
  • 43
  • 1
  • 1
  • 8
  • Problem with the ssl certificate, if the site is localhost you can try `http` instead of `https` but if you need ssl and the site is yours then use a proper [CA certificate](https://letsencrypt.org/) – HMR Mar 27 '18 at 08:40
  • Possible duplicate of [Getting net::ERR\_INSECURE\_RESPONSE when calling url using fetch api](https://stackoverflow.com/questions/49504887/getting-neterr-insecure-response-when-calling-url-using-fetch-api) – HMR Mar 27 '18 at 08:43
  • 4
    Nice, a possible duplicate to a question without an answer. – Yanick Rochon Jan 07 '20 at 15:53

1 Answers1

-2

If you are using NPM then you can install Axios and ignore SSL errors like below.

// At instance level
const instance = axios.create({
  httpsAgent: new https.Agent({  
    rejectUnauthorized: false
  })
});
instance.get('https://something.com/foo');

// At request level
const agent = new https.Agent({  
  rejectUnauthorized: false
});
axios.get('https://something.com/foo', { httpsAgent: agent });

But I think you cannot bypass SSL error if you are running Javascript on browser as it will be blocked by Security layer of the Browser. Please have a look into this issue. Bypassing SSL certificate error in jQuery and AJAX

Actung
  • 1,438
  • 1
  • 13
  • 18
  • 1
    AFAIK an answer that says "use something else" without giving a reason why it does not work, or even try to find a solution is not a good answer. – Yanick Rochon Jan 07 '20 at 15:52
  • @YanickRochon I already mentioned the reason of why it does not work `if you are running Javascript on browser as it will be blocked by Security layer of the Browser` why you get this error is either because the server has invalid certificate due to expiry or invalid certificate data. I thought its already a known thing for this issue. The quesiton clearly asks, how to access not why its not accessible. – Actung Jan 13 '20 at 10:54
  • thank you for the reply, invalid SSL is not the issue for me. The certificate is self-signed, and the browser can access the site just fine (although it asks for unsafe navigation, which can easily be avoided by adding it to the trusted site). Unfortunately, `fetch` still fails regardless. – Yanick Rochon Jan 13 '20 at 13:48
  • this code won't even run in the browser to have a chance of failing, because there is no such symbol `https` – Michael Jun 04 '20 at 01:10