Search code examples
react-nativeapistrapi

React Native not allowing public route call to local strapi server


fairly new with strapi and react native. I created a strapi server and created a route that is public, with public authorization to find and find one(Which i'm guessing is the necessary ones to be public) that works perfectly fine if I call it on postman, even if I type it in the browser, and does not require any authorization or header, but when calling it from my react native app on android simulator it gives me a 401 error, which is strange because I can't find any other way I could possible make it more public, but I'm guessing I'm missing something has anyone had this problem?

Another thing that is strange is when running my react native in web it works fine... it's just on the android simulator

This is my call on my react native!

    useEffect(() => {
    const fetchAllUpdates = async () => {
      await axios
        .get(`http://localhost:1337/api/updates`, {
          headers: {
            'Content-Type': 'application/json',
          },
        })
        .then((res) => {
          console.log('Data From Strapi ===>', res);
          dispatch(setUpdates(res.data.data));
        })
        .catch((error) => console.log(`Strapi Error===> ${error}`));
    };

    fetchAllUpdates();
  }, []);

Thank you in advance!


Solution

  • Ok I have figured it, the issue is that the android simulator does not have access to the local server the same way as the browser... so instead of calling

    http://localhost:1337/api/updates
    

    One has to find the machine ip address, and we can do that by typing

    ipConfig 
    

    into the terminal and you will get your ip address then replace the

    localhost
    

    with the ip and it works

    You should have something like this at the end

    http://192.168.7.469:1337/api/updates