React Native使用axios允许自签名证书 [英] React Native allow self signed certificates using axios

查看:589
本文介绍了React Native使用axios允许自签名证书的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用React Native中的Axios对https://资源进行API调用.

I'm trying to make an API call to a https:// resource using Axios inside React Native.

我正在拨打电话,如下所示:

I'm making the call as follows:

const instance = axios.create({
            httpsAgent: new https.Agent({
                rejectUnauthorized: false
            })
        });


        instance.get('https://foodspecials-api.local/api/v1/specials/2')
            .then((response) => {
                console.log('WORKED');
                console.log(response);
            })
            .catch((e) => {
                console.log('FAILED');
                console.log(e);
            });

但是,这会导致错误消息 ReferenceError:找不到变量:https

This, however, results in an error message ReferenceError: Can't find variable: https

我已经创建了一个自签名的SSL证书.

I've created a SSL certificate which is self-signed.

是否有解决方法?

推荐答案

您应该导入'https'模块import https from 'https',为此需要安装此react-native模块

you should import 'https' module import https from 'https', to do this install this react-native module node-libs-react-native.

这篇关于React Native使用axios允许自签名证书的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆