0

I am using axios in react native project but at the time of post request through axios it gives an error

export const login = (email, password) => async dispatch => {
    const config = {
        headers: {
            'Content-Type': 'application/json'
        }
    };
    
    const body = JSON.stringify({ email, password });
    console.log(body)

        const res = await axios.post(`http://localhost:8000/auth/jwt/create/`, body, config);
        console.log('kk');
        dispatch({
            type: LOGIN_SUCCESS,
            payload: res.data
        });

        dispatch(load_user());
     
};

On postman this axios request is executing successfully and on the same backend address my react JS project is working correctly and this axios request also get successful.

On react native project it gives following error. error

I've also tried try and catch blocks but after execting axios.post line it goes to catch block .

I've tried the same request on Postman and it's successfully sent the post request and get the tokens PostMan

  • 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) – Guruparan Giritharan Sep 05 '21 at 18:49

1 Answers1

0

Solution 1

Check this. Maybe you should set android:usesCleartextTraffic="true" in your AndroidManifest.xml file.

Solution 2

I could suggest the next command which I personally always use when developing RN apps on Android using local emulators. adb reverse tcp:8081 tcp:8081.

Solution 3

Use concrete local ip-address instead of localhost alias. Check this.

  • Sir I'm using an expo project on react native. and connecting my own android device. not any emulator –  Sep 06 '21 at 04:55
  • Are you using Android device via WiFi or USB connections? Maybe there are some problems with sending request to your machine localhost from Android device (especially when it's connected via WiFi) – Anton Patrushev Sep 06 '21 at 07:33