Request on a local server with Expo App is failing

i’m having a little truble here

I created a Flask server running on my personal computer at IP 192.168.1.3:5000, and by itself, it works very well. I tested the requests using Insomnia and through the web, and everything worked perfectly.

However, when the requests are being made from my Expo application on my smartphone, they do not work. It’s as if the application does not have access to local network requests or something similar.

I did some research and found information about permissions in the app.json file. I made the changes as suggested, but it didn’t work. I still only have access from the desktop.

It’s also worth mentioning that if I access the same endpoint through the smartphone’s web browser, I can see the “hello world” message that I inserted at the “/” route of the server. So, I don’t believe it’s a problem with my machine’s IP or the communication between devices within the local network.

Here’s some code:

My flask server:

from flask import Flask, request
from ctypes import windll
from comtypes import CLSCTX_ALL
from pycaw.pycaw import AudioUtilities, IAudioEndpointVolume
from flask_cors import CORS
import requests

app = Flask(__name__)
CORS(app, resources={r"/*": {"origins": "*"}})

VLC_PASSWORD = 'vlcpassword'
VLC_URL = 'http://localhost:8080/requests/status.json'
VK_MEDIA_PLAY_PAUSE = 0xB3
VK_VOLUME_UP = 0xAF
VK_VOLUME_DOWN = 0xAE

def press_key(hexKeyCode):
    windll.user32.keybd_event(hexKeyCode, 0, 0, 0)
    windll.user32.keybd_event(hexKeyCode, 0, 2, 0)

def set_volume(level):
    devices = AudioUtilities.GetSpeakers()
    interface = devices.Activate(IAudioEndpointVolume._iid_, CLSCTX_ALL, None)
    volume = interface.QueryInterface(IAudioEndpointVolume)
    volume.SetMasterVolumeLevelScalar(level, None)

def send_vlc_command(command):
  url = f'http://localhost:8080/requests/status.json?command={command}'
  response = requests.get(url, auth=('', VLC_PASSWORD))
  return response.status_code == 200

@app.route('/play_pause', methods=['POST'])
def play_pause():
  data = request.get_json()
  is_vlc = data.get('is_vlc', "False")
  if is_vlc == "True":
    send_vlc_command('pl_pause')
    return 'VLC play/pause triggered'
  else:
    press_key(VK_MEDIA_PLAY_PAUSE)
    return 'Play/pause triggered'

@app.route('/volume_up', methods=['POST'])
def volume_up():
  data = request.get_json()
  is_vlc = data.get('is_vlc', False)
  if is_vlc == "True":
    send_vlc_command('volume&val=%2B20')
    return 'VLC volume up triggered'
  else:
    press_key(VK_VOLUME_UP)
    return 'Volume up triggered'

@app.route('/volume_down', methods=['POST'])
def vlc_volume_down():
  data = request.get_json()
  is_vlc = data.get('is_vlc', False)
  if is_vlc == "True":
    send_vlc_command('volume&val=-20')
    return 'VLC volume down triggered'
  else:
    press_key(VK_VOLUME_DOWN)
    return 'Volume down triggered'
  
@app.route('/')
def hello():
  return "Hello, World!"

if __name__ == '__main__':
    app.run(host='192.168.1.3', port=5000)

my index.tsx from my expo app:

import React, { useState } from 'react';
import CustomButton from '../../components/CustomButton';
import { StyleSheet, View, Text, Switch } from 'react-native';
import axios from 'axios';

const TabOneScreen = () => {
  const [isVLC, setIsVLC] = useState(false); 
  const [serverIsOnline, setServerIsOnline] = useState(false); 
  const [log, setLog] = useState<any[]>([]); 
  const vlcServerURL = 'http://192.168.1.3:5000'; 

  const handlePlayPause = async () => {
    try {
      const response = await axios.post(vlcServerURL + '/play_pause', {
        is_vlc: isVLC ? 'True' : 'False'
      });
      console.log(response.data);
      setLog([...log, response.data]);
      setLog([...log, response.status]);
    } catch (error) {
      console.error('Fail:', error);
    }
  };

  const handleVolumeUp = async () => {
    try {
      const response = await axios.post(vlcServerURL + '/volume_up', {
          is_vlc: isVLC ? 'True' : 'False',
      });
      console.log(response.data);
    } catch (error) {
      console.error('Fail:', error);
    }
  };

  const handleVolumeDown = async () => {
    try {
      const response = await axios.post(vlcServerURL + '/volume_down', {
        is_vlc: isVLC ? 'True' : 'False'
      });
      console.log(response.data);
    } catch (error) {
      console.error('Fail:', error);
    }
  };

  const testConnection = async () => {
    try {
      console.log(vlcServerURL)
      const response = await axios.get(vlcServerURL + '/');
      setLog([...log, response.data]);
      if (response.status === 200) {
        setServerIsOnline(true);
      }
    } catch (error) {
      console.error('Failure on connection:', error);
    }
  };

  React.useEffect(() => {
    testConnection();
  }, []);

  return (
   visual things here ...
  );
};



export default TabOneScreen;

my app.json android permissions:

"android": {
      "adaptiveIcon": {
        "foregroundImage": "./assets/images/adaptive-icon.png",
        "backgroundColor": "#ffffff"
      },
      "package": "com.teago.tvpccontrol",
      "permissions": ["INTERNET", "ACCESS_NETWORK_STATE"]
    },`

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật