Recently I was building aflutter app and was using Django to build its backend. I wanted to call by APIs from my app which was running on my phone and it was proving to be a hassle. However, after searching the net I finally found the way!!.
Let’s get started.
First, you need to make sure that your computer and phone are connected to the same Wifi network. Alternatively, you can connect your computer to your mobile HotSpot.
Find the IP address of your Desktop.
You can Type the Windows ComandLine
ipconfig
If you’re using a Linux or Mac operating system, type the following command in the terminal
ifconfig
The command will show you your IP Address (Example = 192.168.20.22)
Naturally, when we run our Django server using Python:
python manage.py runserver
It runs on localHost( 127.0.0.1) which we cannot access from another device. So, instead, we will run our server on our computer’s IP Address.
In Django, we do that by running the following command:
python manage.py runserver 0.0.0.0:8000
This command will run our server on our computer so that other devices on the same network can also access the server. Also, don't forget to add your IP address to the ALLOWED_HOSTS in the setting.py file of your Django project.
Now open any browser and open http://YOUR_IP:8000/api_endpoint
For example:= http://192.168.20.22/my_site
You will be able to see the expected result displayed.
The Above example is tailored to Django but it can be done on any other framework. All you have to do is figure out how to run your server on your machine's IP address and connect to the same network. You can use the URL with the IP address and call from any application you are building.