Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Network request failed (device and emulator) #1606

Closed
viniciuscastelano opened this issue Apr 4, 2018 · 14 comments
Closed

Network request failed (device and emulator) #1606

viniciuscastelano opened this issue Apr 4, 2018 · 14 comments
Labels
needs more info To be used when awaiting reporter response

Comments

@viniciuscastelano
Copy link

Hello,

I am getting the error "Network request failed" (device and emulator) while accessing the URL https://reqweb.cast.com.br:5443/RequerimentoRest/req/login via fetch (). I can access this link via Postman or browser normally.

I wonder if there is a solution for this.

My code is:

import React from 'react';
import { Text, View } from 'react-native';

export default class App extends React.Component {
    componentDidMount(){
        return fetch('https://reqweb.cast.com.br:5443/RequerimentoRest/req/login')
            .then((response) => response.json())
            .then((responseJson) => {
                console.log(responseJson);
            }).catch((error) =>{
                console.error(error);
                alert(error);
            });
    }

    render() {
        return (
            <View style={{flex: 1, paddingTop:20}}>
                <Text>Test Rest</Text>
            </View>
        );
    }
}

Thank you very much in advance.

@ide
Copy link
Member

ide commented Apr 4, 2018

The port for HTTPS is typically 443, you may want to double-check that. Additionally, if you are experiencing the issue on Android, the system uses a different set of root CAs than Chrome does, so you may want to verify that your TLS certificate is (transitively) signed by a root CA installed on your system: https://tamingthedroid.com/trusted-credentials

@esamelson esamelson added the needs more info To be used when awaiting reporter response label Apr 5, 2018
@ljtijhuis
Copy link

Hi guys, running into the same issue when using a localhost dev certificate. Any advise on how to make it accept it?

@aprilmintacpineda
Copy link

I run on the same issue. Here's what the situation looks like:

frontend

axios.post('http://localhost:3000/authenticate/login', {
  testData: 'test data value!'
})
.catch(err => console.dir(err));

express backend

import express from 'express';

const app = express();

app.post('/authenticate/login', (request, response) => {
  console.log(request.body);

  response.sendStatus(200);
});

app.listen('3000', () => {
  console.log('Dev server running at http://localhost:3000 or http://10.0.2.2 for android emulator');
});
Error: Network Error

I found some articles on stackoverflow and a lot more from my duckduckgo search but I wasn't able to find any answer.

NOTE
  • I'm running my app via the expo app downloaded on my android phone. I'm not using any VMs to run my frontend and backend.
  • I have tried 10.0.2.2:3000. Same result.
  • I have tried my own IP. Same result.
  • I have tried to search the best places that I know of, but I couldn't find any answers.

Help please.

@aprilmintacpineda
Copy link

I think it would be good to add these kind of info to the docs. "Making network requests to your localhost". Then add "Using an emulator." and "Using your phone.".

@aprilmintacpineda
Copy link

@aprilmintacpineda
Copy link

I'm using the CLI, and I can't see any instructions how to actually configure this thing.

@aprilmintacpineda
Copy link

aprilmintacpineda commented May 2, 2018

@viniciuscastelano
@ljtijhuis
@ide

After going to sleep and upon waking up, I figured out what the answer was. I seem to have forgotten networking fundamentals.

What's going on here?

I'm pretty new to expo and how it managed to push my app on my phone with wireless connection was a magic at first until I remembered networking. Basically expo pushes the app using the "lan" connection (in my case), your PC and the mobile you are using must be connected to the same network for this to work. Don't get me wrong I don't fully understand how expo works on the foundation level though I'd love to know the secret spell for its magic. (LOLS).

My computer is running the localhost server (nodejs) on localhost port 3000 (localhost:3000), so I need to make a request to the computer's port 3000 in order for this to work, unfortunately for me, I was getting my IP thru https://duckduckgo.com/?q=what%27s+my+ip%3F&atb=v113-7_y which didn't work because I'm running on LAN so it needs to be the IP on the LAN network. So what you need is LANIP:3000/path/to/request for this network request to work.

Find out what your IP is: https://www.wikihow.com/Check-the-IP-Address-in-Linux

Now that you know your IP address, instead of doing http://localhost:3000/a-request do http://TheIpGoesHere:3000/a-request, in this case, your phone will make a request to your PC's port 3000 where the localhost is running. Example: http://192.168.1.2:3000/login.

image

If someone can explain this better or can add more information as to why the other works and why the other ones don't, please mention me.

BTW, I'm using the terminal so to run expo in LAN mode you should do: exp start --lan add a -c to clear the cache. More info at https://docs.expo.io/versions/v27.0.0/workflow/exp-cli. After running in LAN mode you should a url that's something like this: 11:46:00 [exp] Your URL is: exp://192.168.1.2:19000

I hope this helps.

@cadec
Copy link

cadec commented Aug 8, 2018

@aprilmintacpineda I have been dealing with this issue for SO long and this is the first time I was able to successfully make a network request to my localhost!!! Thank you SOOO much!!!

@chirag773
Copy link

@aprilmintacpineda Hey Im still facing the problem even i replace my localhost to my IP . Done all the things you have told to do to run the app but Im still facing the problem please help me out

@sjchmiela
Copy link
Contributor

@chirag773 My suggestion would be to check if the server and the device are in the same network (if they're not, the IP won't work).

Thank you, @aprilmintacpineda, for the detailed explanation of how to make it work! 🙂

@melitus
Copy link

melitus commented Dec 12, 2018

@sjchmiela the server and the device are on the same network but i am still getting the error

@AdamZaczek
Copy link

AdamZaczek commented Jan 1, 2019

I actually had the exact same issue and none of the solutions worked for me. I'm using expo 30. For now, I ended up deploying the backend and using remote URL but an offline solution is something I really need. @aprilmintacpineda 's answer is excellent (thanks man!)and to my surprise, even that did not help.

@AleksandarSavic95
Copy link

What helps when testing from a real device is to use a service like ngrok. It is easy to setup (3 minutes or so).
After that, just change the url in your config from localhost to something like http://44dd0123.ngrok.io
Good luck :)

@aleksanderkaminski
Copy link

☝️ @AleksandarSavic95 I had a problem with connecting to local server from iPhone expo client, used ngrok and it works perfectly. Thanks!

@lock lock bot added the outdated label Nov 22, 2019
@lock lock bot locked and limited conversation to collaborators Nov 22, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
needs more info To be used when awaiting reporter response
Projects
None yet
Development

No branches or pull requests