0

I'm trying to have a form on React Native send a POST request to register a new user. The backend is an https Express server which can take requests just fine with Postman. Only when I try it on an emulator or on Expo it says AxiosError: Network Error. I've been looking at different solutions that say to change the IP from localhost which led to the same issue.

  //State of the form
  const [loginState, setLoginState] = useState(1);


  //Set base URL
  axios.defaults.baseURL = 'https://127.0.0.1:5000';


  //Data from user when they fill out the form
  const [email, setEmail] = useState('');
  const [username, setUsername] = useState('');
  const [firstName, setFirstName] = useState('');
  const [lastName, setLastName] = useState('');
  const [password, setPassword] = useState('');
  const [verifyPassword, setVerifyPassword] = useState('');


  const handleForm = (loginState, setLoginState) => {
    setLoginState(loginState + 1);
    console.log(email);
  }

  //Send post request with user submitted data
  const onSubmitForm = async e => {
    e.preventDefault();
    try{
      await axios(
        {
          method: 'POST',
          url: '/authentication/register',
          data: {
            email: email,
            username: username,
            firstName: firstName,
            lastName: lastName,
            password: password
          }
        }
      );

      console.log(response.json)
    }
    catch (error){
      console.error(error);
    }
  }
Kales35
  • 11
  • 2
  • instead of using localhost IP address, use ipv4 IP address then it will work fine. – Asad Sep 16 '22 at 19:49
  • Does this answer your question? [React Native Android Fetch failing on connection to local API](https://stackoverflow.com/questions/33704130/react-native-android-fetch-failing-on-connection-to-local-api) – Abe Sep 16 '22 at 19:51
  • axios.defaults.baseURL = 'https://127.0.0.1:5000'; just it with ipv4 address, axios.defaults.baseURL = 'http://192.168.----'; – Asad Sep 16 '22 at 19:51
  • @Asad Thank you! I used my ipv4 address but it's giving me a 401 error. I changed the server from an https to an http too. – Kales35 Sep 21 '22 at 16:15
  • @Kales35, First of all, test your call in postman by using ipv4 address then call it in you app – Asad Sep 21 '22 at 17:46

0 Answers0