7

I'm writing an app in React Native, and I need to make API requests. The server for that is running on a server with a self-signed SSL certificate, so I need Axios and React Native to make the request and accept the response using a self-signed certificate.

  • I can't get a certificate from an official certificate authority.
  • I have already tried using https.Agent, however this doesn't seem to be possible when using React Native (The https module is part of the node standard library, which is not included in React Native), so this is also not a valid solution.
  • I would prefer to continue using Axios, however I would consider switching libraries if there are any with similar functionality (I've already tried a few, none provided functionality to allow self-signed certs).

Here is some example code from my project (url is the IP of the server):

import axios from 'axios';

const instance = axios.create({ });
instance.get(url)
    .then(res => {
        this.setState({dataSource: res.data});
     })

Btw, when I'm using ngrok everything works fine, so the self-signed certificate is definitely the only problem.

Zoe
  • 27,060
  • 21
  • 118
  • 148
b3nj4m1n
  • 502
  • 7
  • 24

1 Answers1

0

I solved this problem today! I too have a react native app, and i use apisauce, an awesome lib that uses axios under the hood.

My API runs on a LAMP stack locally, and I have a fake development domain in /etc/hosts, and Apache is configured with a self signed certificate.

Instead of disabling the SSL verification, trust your certificate!

The instructions will differ on Android, but for iOS I did the following.

  • Send the selfsigned.crt to my iPhone via Airdrop
  • Install the profile in the Settings app
  • Download and launch Proxyman on my dev machine https://proxyman.io/
  • Set Proxy in Wifi settings on the iPhone (dev machine IP port 9090)
  • Open Safari and goto proxy.man/ssl, download the certificate
  • Install the profile in the Settings app
  • Enable full trust for both certificates in Settings > General > About > Certificate Trust Settings

Now you should be able to access https://your-dev-domain.whatever without any problems, and apisauce/axios will no longer return Network Error

Just remember to go back into your wifi settings and turn the proxy off again when you are finished developing!

delboy1978uk
  • 12,118
  • 2
  • 21
  • 39