【问题标题】:Self signed certificate React Native自签名证书 React Native
【发布时间】:2018-11-26 07:09:28
【问题描述】:

我尝试在 React Native 的调用 API 中导入自签名证书(certificate.crt),但每次都出现同样的错误:

Unable to resolve module `../certificate.crt`

我正在将 React Native 与 Redux 和 Redux Saga 一起使用

import axios from "axios";

const casert = require("../certificate.crt");

const API_URL = "MY_IP_SERVER";

export function callGetApi(url, param) {
    return axios({
    method: "get",
    ca: casert,
    url: `${API_URL}${url}${param}`,
    headers: {
        'Accept': 'application/json',
        'Content-Type': 'application/json'
    }
});
}

文件树是这样的:

如果有人知道如何包含此自签名证书,因为 API 需要在标头中接受任何请求。

谢谢

【问题讨论】:

    标签: react-native react-native-android redux-saga


    【解决方案1】:

    当你使用axios时,看看:

    How can I make a HTTPS to the backend with a self-signed certificate?

    完成所有步骤,然后你就有了这样的东西:

      <Button onPress={
        ()=>{
          const x = axios.create({
            baseURL: 'https://api.realtycoast.io/',
            timeout: 10000,
            headers: {
              'Accept': 'application/json',
              'Content-Type': 'application/json',
            }
          });
        x.request({
          url: '/user/123'
        })
          .then(function (response) {
            console.log(response);
          })
          .catch(function (error) {
            console.log(error);
          });
        }
      }>
        <Text>Axios Test</Text>
      </Button>
    

    【讨论】:

    • 谢谢 MohamadKh75,但我需要在客户端的请求中包含证书,以便 API 作为“认证客户端”接受此 API 的请求。
    【解决方案2】:

    我找到了答案的开头:https://github.com/axios/axios/issues/1495

    import axios from "axios";
    
    const API_URL = "HTTPS://MY_IP_SERVER";
    
    export function callGetApi(url, param) {
        return axios({
            method: "get",
            httpsAgent: new https.Agent({
                ca: fs.readFileSync("../certificate.crt"),
            }),
            url: `${API_URL}${url}${param}`,
            headers: {
                'Accept': 'application/json',
                'Content-Type': 'application/json'
            }
        });
    }
    

    知道,证书包含在我的传奇函数中,当我尝试执行 API 调用时直接进入 catch 异常

    export function* loginRequest(payload) {
    
        yield takeEvery(authActions.LOGIN_REQUEST, function*(payload) {
            try {
    
                console.log("Email Saga : " + payload.email);
                let response = null;
    
                response = yield call(requestUUID, "account/uuid/email/", payload.email);
                const uuid = response.data.uuid;
    
                debugger;
    
                yield put({ type: "LOGIN_SUCCESS" });
            } catch (error) {
                debugger;
                yield put({ type: authActions.LOGIN_ERROR });
            }
        });
    }
    

    【讨论】:

      猜你喜欢
      • 2017-06-19
      • 1970-01-01
      • 2021-05-08
      • 2019-03-01
      • 2016-07-17
      • 2021-01-11
      • 1970-01-01
      • 1970-01-01
      • 2020-08-04
      相关资源
      最近更新 更多