Home > Back-end >  How to connect to an API running on localhost from a React Native App running on an iPhone
How to connect to an API running on localhost from a React Native App running on an iPhone

Time:09-15

fairly new to all this but I'm trying to get my React Native app to connect to a .NET Core API running on my localhost. I want to test on an iPhone so to connect I am using the QR code that is generated when starting the React Native app using Expo Client

The URL I tried using to connect to the API is:

https://localhost:51787

I also tried using the local IP address from Ipconfig

http://192.168.1.69:51787

And from another post that suggested trying this for iPhones :

http://10.0.2.2:51787

This is how I'm using it :-

  const login = () => {

    fetch('https://localhost:51787/api/Login?username='   username   '&password='   password, {
      method: 'POST',
      headers: {
        Accept: 'application/json',
        'Content-Type': 'application/json'
      }
    })
    .then((response) => response.json())
    .then((responseJson) => {
    
        // Do stuff here

    })
    .catch((error) => {
      console.error(error);
    });
  }

Unfortunately the URLs that I tried do not work and I get the error 'Network Request Failed'

I have enabled Cross-Origin Request.

Can anyone point me in the right direction, thanks

CodePudding user response:

You could try with the node package localtunnel.

Localtunnel

You install it global and start it up pointing to the port your api is running on.

lt --port 8080

Then you get an url to point to from the outside world.

your url is: https://salty-beans-sneeze-87-***-***-191.loca.lt

I used it to test webhook functions from other websites redirecting to my localhost.

  • Related