question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Network request failed (device and emulator)

See original GitHub issue

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.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:5
  • Comments:14 (2 by maintainers)

github_iconTop GitHub Comments

44reactions
aprilmintacpinedacommented, 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’s+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.

11reactions
aprilmintacpinedacommented, May 1, 2018

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.”.

Read more comments on GitHub >

github_iconTop Results From Across the Web

React-Native Network request failed on Android Emulator API ...
I'm seeing this on a physical device running Android Pie / Android API Level 28. Also saw this on the iOS Simulator. This...
Read more >
Network request failed Android - Stack Overflow
Check your Android emulator options panel adjacent to your emulator, in that you will have more options then go to cellular and check...
Read more >
How to solve 'TypeError: Network request failed' in React ...
two thing : 1. add android:usesCleartextTraffic="true" in your Android manifest android/app/src/main/AndroidManifest.xml2.
Read more >
Network request Failed. Linux with Android-Studio, ExpoGo ...
I'm running pop_os(Linux) and using expoGo with Metro bundler and Android-Studio emulator...I keep getting. Network request failed at ...
Read more >
Network Request Failed/Network Error in React Native using ...
When making an HTTP Request with React Native, there is some kind of conflict because your Android app is run on an emulator...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found