How to configure axios to use SSL certificate?

前端 未结 4 1608
广开言路
广开言路 2020-12-02 12:39

I\'m trying to make a request with axios to an api endpoint and I\'m getting the following error: Error: unable to verify the first certificate

It seems

4条回答
  •  春和景丽
    2020-12-02 13:08

    For me, when the my application is running in development mode, I have disabled the rejectUnauthorized directlly in axios.defaults.options. This work very weel. Don't do this in production mode.

    import https from 'https'
    import axios from 'axios'
    import config from '~/config'
    
    /**
     * Axios default settings
     */
    axios.defaults.baseURL = config.apiURL
    
    /**
     * Disable only in development mode
     */
    if (process.env.NODE_ENV === 'development') {
      const httpsAgent = new https.Agent({
        rejectUnauthorized: false,
      })
      axios.defaults.options = httpsAgent
      // eslint-disable-next-line no-console
      console.log(process.env.NODE_ENV, `RejectUnauthorized is disabled.`)
    }
    

提交回复
热议问题