0

When I try to post data with axios it gives a network error without any information. I'm using python fast api as the backend and it works fine. I also tried to change my network address from ip address to localhost address but it also didn't work.

error log

 LOG  [AxiosError: Network Error]
 LOG  [AxiosError: Network Error]
 LOG  [AxiosError: Network Error]
 LOG  [AxiosError: Network Error]

react native code

const handleSubmit = () => {
    const params = {
      id,
      month,
      day,
      time,
    };

    axios
      .post("https://0.0.0.0:8080/prediction", params)
      .then((res) => {
        const data = res.data.data;
        const parameters = JSON.stringify(params);
        const msg = `Parameters: ${parameters}\nPrediction: ${data.prediction}`;
        alert(msg);
      })
      .catch((error) => console.log(error));
  };

  return (
    <View style={styles.view1}>
      <Text style={{ textAlign: "center" }}>Time predictor</Text>

      <View style={styles.inputview}>
        <TextInput label="ID" value={id} onChangeText={(text) => setId(text)} />
      </View>

      <View style={styles.inputview}>
        <TextInput
          label="Month"
          value={month}
          onChangeText={(text) => setMonth(text)}
        />
      </View>

      <View style={styles.inputview}>
        <TextInput
          label="Day"
          value={day}
          onChangeText={(text) => setDay(text)}
        />
      </View>

      <View style={styles.inputview}>
        <TextInput
          label="Time"
          value={time}
          onChangeText={(text) => setTime(text)}
        />
      </View>

      <TouchableOpacity style={styles.btn} onPress={() => handleSubmit()}>
        <Text style={styles.btnText}>Submit</Text>
      </TouchableOpacity>
    </View>
  );
}

python code

@app.post("/prediction")
async def get_predict(data: Item):
    feature_list = []

    feature_list.append(int(data.id))
    feature_list.append(int(data.month))
    feature_list.append(int(data.day))
    feature_list.append(int(data.time))

    pred_value = prediction(feature_list)

    return {
        "data": {
            'prediction': pred_value
        }
    }

if __name__ == '__main__':
    uvicorn.run(app, port=8080, host='0.0.0.0')

Any help will be greatly appreciated.

AIB33
  • 11
  • 4
  • Try passing headers - axios.post('url', {"body":data}, { headers: { 'Content-Type': 'application/json' } } ) – Kailash Sep 26 '22 at 05:00
  • Are you trying in real device or emulator/Simulator - make sure both your system and testing device/simulator/emulator is connected to the same wifi or network. – Kailash Sep 26 '22 at 05:46
  • @Kailash I tried it but still getting the same error. There is no other information in the console. – AIB33 Sep 26 '22 at 05:49
  • `axios({ method: 'post', url: '/login', data: { firstName: 'Finn', lastName: 'Williams' } });` try this way once. – Kailash Sep 26 '22 at 05:51
  • @Kailash I'm using both web browser and the expo app to testing the app and all are in the same wifi network. And I'm using linux as my development environment. – AIB33 Sep 26 '22 at 05:51
  • Does your api working in postman ? I'm doubting your api url as well. – Kailash Sep 26 '22 at 05:54
  • @Kailash Yes api is working fine. Also I tried it with the react.js it also working there correctly, but the problem occurs only in the react native. I wrote the above code like that axios({ method: "post", url: "http://0.0.0.0:8080/prediction", data: { id: id, month: month, day: day, time: time }, }) but no luck – AIB33 Sep 26 '22 at 06:04
  • You have added wrong url , use - `url: "https://0.0.0.0:8080/prediction"` , If doesn't work then try with `http` once. – Kailash Sep 26 '22 at 06:29
  • At last do follow https://stackoverflow.com/questions/49370747/network-error-with-axios-and-react-native hope it will work for you. – Kailash Sep 26 '22 at 06:33

0 Answers0