Axios: Setting rejectUnauthorized to false using httpsAgent to ignore SSL issues doesn’t work with POST requests

  Kiến thức lập trình

I’m using axios with NodeJS to query the REST API of Mikrotik RouterOS for that you need to use self-signed SSL certificates mandatorily.
It all works fine when i run GET requests and add httpsAgent: new https.Agent({ rejectUnauthorized: false}) .
But all other methods fail and I get this error:

cause: Error: self-signed certificate in certificate chain
      at TLSSocket.onConnectSecure (node:_tls_wrap:1674:34)
      at TLSSocket.emit (node:events:518:28)
      at TLSSocket._finishInit (node:_tls_wrap:1085:8)
      at ssl.onhandshakedone (node:_tls_wrap:871:12) {
    code: 'SELF_SIGNED_CERT_IN_CHAIN'
  }

i’ve already tried setting process.env['NODE_TLS_REJECT_UNAUTHORIZED'] = 0; but with that it gives me a 401 HTTP code with “Unauthorized” message.

const macAddress= "10-78-D2-F9-12-E7";
const byPassed= "bypassed";


axios.post(`https://192.168.8.1:8082/rest/ip/hotspot/ip-binding`,
    {
        httpsAgent: new https.Agent({ rejectUnauthorized: false}), // Allow Self-signed certificates
        auth: {
            username: "****",
            password: "*****"
          },
          headers: {
            'Content-Type': 'application/json',
        },
        content: JSON.stringify({
            "mac-address": macAddress,
            "type": byPassed,
            
        })
    },
    
    ).then(response => {
        console.log(response.data)
    }).catch(error => {
        console.log(error)
    });

New contributor

Vebama Wanematou is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Theme wordpress giá rẻ Theme wordpress giá rẻ Thiết kế website

LEAVE A COMMENT