10

I have tried to send a POST data to a server but i received an error , “net::ERR_CERT_INVALID”. The code as shown below is what I tried to bypass the error but it’s still fail. Please help advice. Thank you.

import { HttpClient, HttpParams } from '@angular/common/http';

createData(data): Promise<any> {
    let post_message = data;
    let header_node = {
      Accept: 'application/json',
      rejectUnauthorized: 'false',
    }
    // this.oauth_header.Authorization = 'Bearer ' + access_token;
    const url_params = new HttpParams()
      .set('rejectUnauthorized', 'false')
      .set('requestCert', 'false')
      .set('insecure', 'true')

    return this.http.post('https://ip/createdata', post_message, {
      headers: header_node,

    }).toPromise();
1
  • Hi @CBV, could you resolve this error in angular? Commented Feb 27, 2022 at 8:04

3 Answers 3

4

Try passing headers as HttpHeaders to POST method.

import { HttpClient, HttpParams, HttpHeaders } from '@angular/common/http';

createData(data): Promise<any> {

    let post_message = data;
    let header_node = {
        headers: new HttpHeaders(
            { 'Accept': 'application/json' },
            { 'rejectUnauthorized': 'false' })
        };

    return this.http.post('https://ip/createdata', post_message, header_node).toPromise();
}
Sign up to request clarification or add additional context in comments.

Comments

1

it's been a while since this post was posted, nevertheless, I ran into a similar problem with a keycloak dev server (self-signed cert = invalid), I was building an angular 13+ app to interact with it. As I asume you were receiving this error from browser, let me say that it's not kind of HttpClient or Angular problem, but the browser blocking the process, then for future cases, here is the "solution/bypass":

Go to the browser, type your server URL/IP (the invalid certificate one), and then, when invalid cert browser alert prompts out, allow access for the browser to navigate there. I've granted access by the image below but also in privacy&security settings (chrome tested) Browser screenshot example

For me it worked, hope it helps for people like me few years later trying to solve the same issue!

Comments

0

For anyone that try to bypass the self signed certificate verification in your angular test, I've done it with

=> window._resourceLoader._strictSSL = false;

in before or anywhere else before your calls.

For more info HttpClient from angular use createClient in xhr-utils.js from jsdom that use the XMLHttpRequestImpl class in test and in the class we have

class XMLHttpRequestImpl extends XMLHttpRequestEventTargetImpl {
    [...]
    this.flag = {
      [...]
      strictSSL: window._resourceLoader._strictSSL,
      [...]
    };
    [...]
}

strictSSL to false is the key to disabled the verification as is it used by createClient in xhr-utils.js from jsdom to disabled ssl verifications.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.