user9571141
user9571141

Reputation: 123

Call localhost API within react native ( android device : connected through local)

Have created a simple GET API using node.js and trying to consume it within my expo-react native project using axios/fetch.

The GET API is called whenever user clicks on submit button.

Submit -> http://localhost:port/api

However, when I am trying to test the feature on my android device by connecting the device through datacable and selecting "local" connect type in expo, the API call always falls under exception.

When I tried to log the exception, the exception is printed empty.

I double checked the API and it works fine when tested through postman.

I tried replacing localhost url with my-Ip (http://IP:port/api) and also with 127.0.0.1/127.0.2.2 (http://127.0.0.1:port/api , http://127.0.2.2:port/api) , but nothing seems to make the API call a success.

When I replaced the localhost URL with Facebook's network sample API, the application worked just fine. So, there is no issue with the API calling method.

Also, tried adding the port to windows inbound firewall request but that didn't work too.

Can someone help me out with this ?

Upvotes: 8

Views: 14022

Answers (3)

Aljohn Yamaro
Aljohn Yamaro

Reputation: 2881

Good day, This is working

I use my machine's IP the Ethernet adapter Ethernet 2: IPV4 Address

And allow my port on firewall both Inbound and outbound rules

enter image description here

Upvotes: 0

vmf91
vmf91

Reputation: 1937

You must call the API from your local IP address. To get your local IP, just do the following:

If you are on Ubuntu 16.04:

ifconfig | grep 192

If you are on Ubuntu 18.04:

ip -c a | grep 192

Your local IP address will probably be something like 192.168.0.XXX.

Upvotes: 4

Rahmat Anjirabi
Rahmat Anjirabi

Reputation: 968

yes, i also had this problem until use my Local IP (example: http://192.168.1.55:port) instead 127.0.0.1 please see this answer

Upvotes: 3

Related Questions