Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ?

like image 205
user9571141 Avatar asked Feb 03 '19 12:02

user9571141


2 Answers

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

like image 95
Rahmat Anjirabi Avatar answered Oct 10 '22 10:10

Rahmat Anjirabi


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.

like image 40
vmf91 Avatar answered Oct 10 '22 11:10

vmf91