Reputation: 17
I'm running a NodeJS service in my local machine which has some APIs in it that I created. I'm trying to hit those APIs using my react native application but I'm getting the following error:
error [TypeError: Network request failed]
I'm using fetch to make call to my service:
const newUser = fetch('http://localhost:5001/{firebase_service}/us-central1/app/user', {
method: 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json'
},
body: userPayload
})
.then(response => response.text())
.then(result => console.log(result))
.catch(error => console.log('error', error));
Can anyone help me get unblocked ?
Upvotes: 0
Views: 2679
Reputation: 714
The problem is in the localhost because localhost refers to your emulator. change localhost to 10.0.2.2 so your URL should be 10.0.2.2:5001{firebase_service}/us-central1/app/user
Upvotes: 3
Reputation: 118
I see you're trying to connect to your local server from your app. but as Majid lotfinia pointed out, it is possible that localhost:5001 is pointing to the emulator (or your phone if you're using your physical device) rather than your desktop. Also in many cases react native does not work with http websites, and https is mandatory. So i'd suggest you use Ngrok for testing , until you have moved your server to a cloud.
Upvotes: 0