24

I have created an API endpoint using the Django python framework that I host externally. I can access my endpoint from a browser (mydomain.com/endpoint/) and verify that there is no error. The same is true when I run my test django server on locally on my development machine (localhost:8000/endpoint/). When I use my localhost as an endpoint, my json data comes through without issue. When I use my production domain, axios gets caught up with a network error, and there is not much context that it gives... from the debug console I get this:

Error: Network Error
    at createError (createError.js:16)
    at XMLHttpRequest.handleError (xhr.js:87)
    at XMLHttpRequest.dispatchEvent (event-target.js:172)
    at XMLHttpRequest.setReadyState (XMLHttpRequest.js:554)
    at XMLHttpRequest.__didCompleteResponse (XMLHttpRequest.js:387)
    at XMLHttpRequest.js:493
    at RCTDeviceEventEmitter.emit (EventEmitter.js:181)
    at MessageQueue.__callFunction (MessageQueue.js:353)
    at MessageQueue.js:118
    at MessageQueue.__guardSafe (MessageQueue.js:316)

This is my axios call in my react native component:

    componentDidMount() {
        axios.get('mydomain.com/get/').then(response => {  // localhost:8000/get works
            this.setState({foo:response.data});
        }).catch(error => {
            console.log(error);
        });
    }
smilebomb
  • 4,641
  • 8
  • 41
  • 76
  • You probably want to have some sort of logger to see the request and response to make sure what the actual network error is – Sean Wang Mar 19 '18 at 20:10
  • Is this happening on iOS, Android, or both? – ajthyng Mar 19 '18 at 21:34
  • @SeanWang Is there something you were thinking of? I'm not sure what you mean. – smilebomb Mar 20 '18 at 01:06
  • @ajthyng iOS, haven't tried android. – smilebomb Mar 20 '18 at 01:06
  • 1
    Possible duplicate of [Axios (in React-native) not calling server in localhost](https://stackoverflow.com/questions/42189301/axios-in-react-native-not-calling-server-in-localhost) – Rex Low Mar 20 '18 at 01:28
  • Possible duplicate of [React Native fetch() Network Request Failed](https://stackoverflow.com/questions/38418998/react-native-fetch-network-request-failed) – Michael Cheng Mar 20 '18 at 01:35
  • The solution to these error can vary and the error message is not very helpful. For those that might still run into the same problem, if you want to get helpful error messages that will help you debug, use `console.log(error.response)`. I spent a whole day before I saw this in [axios documentation](https://axios-http.com/docs/handling_errors) – SirG Mar 15 '22 at 22:18

9 Answers9

39

If you are trying to call localhost on android simulator created with AVD, replacing localhost with 10.0.2.2 solved the issue for me.

Esteban Vega
  • 491
  • 5
  • 3
34

It seems that unencrypted network requests are blocked by default in iOS, i.e. https will work, http will not.

From the docs:

By default, iOS will block any request that's not encrypted using SSL. If you need to fetch from a cleartext URL (one that begins with http) you will first need to add an App Transport Security exception.

smilebomb
  • 4,641
  • 8
  • 41
  • 76
18
  1. change from localhost to your ip(192.168.43.49)
  2. add http://

    http://192.168.43.49:3000/user/

kamalesh biswas
  • 751
  • 6
  • 8
12

If you do not find your answer in other posts

In my case, I use Rails for the backend and I tried to make requests to http://localhost:3000 using Axios but every time I got Network Error as a response. Then I found out that I need to make a request to http://10.0.2.2:3000 in the case of the android simulator. For the iOS simulator, it works fine with http://localhost:3000.

Conclusion

use

http://10.0.2.2:3000

instead of

http://localhost:3000
illusionist
  • 9,926
  • 2
  • 59
  • 77
2

For me, the issue was because my Remote URL was incorrect. If you have the URL is a .env file, please crosscheck the naming and also ensure that it's prefixed with REACT_APP_ as react might not be able to find it if named otherwise.

In the .env file Something like REACT_APP_BACKEND_API_URL=https://appurl/api can be accessed as const { REACT_APP_BACKEND_API_URL } = process.env;

Cryce Truly
  • 99
  • 1
  • 5
2

Try "Content-Type": "application/x-www-form-urlencoded", Accept: "application/json"

bhaRATh
  • 489
  • 2
  • 19
1

I was facing the same issue.

i looked deeper and my

endpoint url was not correct.

By giving axios right exact url, my api worked like charm.

Hope it may help anyone

Muhammad Ashfaq
  • 1,835
  • 2
  • 15
  • 39
1

Make sure to change localhost to your_ip_address which you can find by typing ipconfig in Command Prompt

Trying adding to your AndroidManifest.xml

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

enestatli
  • 339
  • 1
  • 9
  • 16
0

If you are using android then open your command prompt and type ipconfig. Then get your ip address and replce it with localhost.

In my case, first I used http://localhost:8080/api/admin/1. Then I changed it to http://192.168.1.10:8080/api/admin/1. It worked for me.