Issue sending POST request on React Native with Axios: AxiosError: Network Error

Viewed 31

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);
    }
  }

enter code here

0 Answers
Related