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

Fetch Fails with "Network request failed" only on Android #16451

Closed
jmedran opened this issue Feb 26, 2022 · 25 comments
Closed

Fetch Fails with "Network request failed" only on Android #16451

jmedran opened this issue Feb 26, 2022 · 25 comments
Labels
incomplete issue: missing or invalid repro A minimal reproducible example is required for most issues

Comments

@jmedran
Copy link

jmedran commented Feb 26, 2022

Summary

Hi Expo Team - On Android async Fetch request fails with an ambiguous "Network request failed" error only on Android. iOS works great. This happens both locally in Expo Go, and in a production apk build on a physical device. Oddly enough, this only happens for ONE endpoint. Currently, I call 4 endpoints using fetch, and 3 work on Android. I highly doubt this is an issue with my server, because the fetch call that fails never makes it to the server. Greatly appreciate any help!

Managed or bare workflow? If you have ios/ or android/ directories in your project, the answer is bare!

managed

What platform(s) does this occur on?

Android

SDK Version (managed workflow only)

SDK 44

Environment

expo-env-info 1.0.2 environment info:
System:
OS: macOS 12.2.1
Shell: 5.8 - /bin/zsh
Binaries:
Node: 14.17.0 - /usr/local/bin/node
Yarn: 1.22.15 - ~/.npm-global/bin/yarn
npm: 6.14.13 - /usr/local/bin/npm
Managers:
CocoaPods: 1.11.2 - /usr/local/bin/pod
SDKs:
iOS SDK:
Platforms: DriverKit 21.2, iOS 15.2, macOS 12.1, tvOS 15.2, watchOS 8.3
IDEs:
Xcode: 13.2.1/13C100 - /usr/bin/xcodebuild
npmPackages:
babel-preset-expo: 9.0.1 => 9.0.1
expo: ^44.0.0 => 44.0.4
react: ^17.0.1 => 17.0.2
react-native: ^0.64.3 => 0.64.3
react-navigation: ^4.4.0 => 4.4.4
npmGlobalPackages:
eas-cli: 0.47.0
expo-cli: 5.2.0
Expo Workflow: managed

Reproducible demo

Fetch call that fails:

export const updateEmailAction = (newEmail) => {
  return dispatch => {
    dispatch(triggerUpdateEmail());
    return getAuthToken(newEmail).then(request => {
      fetch(UPDATE_EMAIL_ENDPOINT, request)
      .then(response => {
        if(response.status === 200){
          dispatch(updateEmailSuccess())
        }else throw Error(response.error);
        })
        .catch(e => {
          console.log("ERROR", e.message);
          dispatch(updateEmailError())
        });
    })
  };
}

where getAuthToken() is:

const getAuthToken = (newEmail) => {
  return firebase.auth().currentUser.getIdToken().then((idToken) =>{
    const request = {
      method: 'POST',
      headers:{
        'Authorization': `Bearer ${idToken}`
      },
      body: newEmail
    }
    return request;
  });
}

Note: getAuthToken works fine and is not the cause of the issue. The issue happens with fetch()

Fetch call that works:

export const deleteAccountAction = () => {
  return dispatch => {
    dispatch(triggerDeleteAccount());
    return getAuthToken().then(request => {
      fetch(DELETE_USER_ENDPOINT, request)
        .then(response => {
          dispatch(deleteAccountSuccess(response))
        })
        .catch(e => {
          console.log(e);
          dispatch(deleteAccountError())
        });
    })
  };
}

As you can see, these fetch calls are essentially configured the same. The main difference is the URL to which they point. As I previously mentioned, the server isn't receiving the request for the failed fetch. Its not rejecting the request, the fetch just isn't happening. So strange. Please help - I've been scouring the internet for days without any lead. The error "Network request failed" is far too ambiguous. Thank you!

@jmedran jmedran added the needs validation Issue needs to be validated label Feb 26, 2022
@jmedran jmedran changed the title Fetch Fails with "Network request failed" on Android Fetch Fails with "Network request failed" only on Android Feb 26, 2022
@edenriquez
Copy link

My team and I spend the last 2 days debugging this after update to expo 44 as well and we saw the same behaviour, we had to rollback to a previous version because of this.

@raajnadar
Copy link
Contributor

I had similar issue with axios and android expo managed, What I did was using flipper to inspect the request & response headers, I found that with axios for api the content type was not correct it was not sending the boundary.

axios/axios#4406 (comment)

axios/axios#4406 (comment)

The best way to debug is using the flipper or remote js debug & compare the working api call request & response with the not working one.

@trayldev
Copy link

trayldev commented May 4, 2022

Are there any updates to this posting? I'm hitting the same snags today, tried rolling back to axios@0.24.0 and expo@42.0.0 with no luck so far. Not sure if this root issue has been fixed since February. Thanks!

@ezratechdev
Copy link

Am facing a similar issue.Am trying to upload a file using form data.Has there been a fix to this?

@hugoqueiros-parfois
Copy link

My app was making the fetch like 2 days ago, and now it gives me this error. It was some expo update? Any updates?

@margins200
Copy link

Another option! scan the QR code with expo clients

@rinathakkar
Copy link

I am facing same issue in "Network request failed" error only on Android in Expo. Below solution works for me both platform (Android and iOS):

#2402 (comment)
https://stackoverflow.com/a/64397804

@Ulrich-HOUNGBO
Copy link

I get the same error for my login form with expo go but when I launch it on the web everything works fine

@abdulmuksith3
Copy link

abdulmuksith3 commented Jun 12, 2022

I suffer from the same issue only on some Android devices. However, on some devices when I completely close the app and re-open the error seems to be gone. Any idea what is wrong or solutions?

Managed Workflow
Expo: 44.0.0
RN: 0.64.3

@WillenOLeal
Copy link

Having the same issue on expo SDK 46

@louischan
Copy link

In my case, the issue was caused by incomplete SSL certificate chain on the web server. Adding the intermediate certificate solved it.

@ewfian
Copy link

ewfian commented Nov 17, 2022

In my case, the issue was caused by incomplete SSL certificate chain on the web server. Adding the intermediate certificate solved it.

Same case here.

@garrettg123
Copy link

Seeing this on Expo v47 (works on iOS). Already using android:usesCleartextTraffic="true".

@dmahajan980
Copy link

dmahajan980 commented Nov 26, 2022

Can confirm this still exists. I'm facing the same issue on Expo SDK 47 (managed workflow) for Android.

@dmahajan980
Copy link

Okay, so I was able to get through this by setting the Content-Type header.

fetch(<SOME_URL>, {
  ...
  headers: {
    ...
    'Content-Type': 'application/json'
  }
})

@danyhiol
Copy link

danyhiol commented Jan 4, 2023

I also have this same issue on both the emulator and the device on android(only).
After some searching and debugging, I could extract the error from adb: java.security.cert.CertPathValidatorException: Trust anchor for certification path not found. And after googling for hours, couldn't find any way of solving this. I ended up thinking this was an issue with the server: https://<domain>:<port>/. But requests to the same server are working on IOS and on WEB or tools such as Postman. Some solutions suggest adding mimeType to the formData option. But I am not posting a file, but some parameters. In fact, my server accepts only formData. Moreover, not all requests are failing.

@garrettg123
Copy link

I saw this error while using localhost in development (which works on iOS). Changing this to my machine's static ip (eg. 192.168.0.100) fixes this on Android, although it's a bit annoying when it changes.

@danyhiol
Copy link

danyhiol commented Jan 4, 2023

@garrettg123 can you elaborate? Where do I make this change? My server is not local, but remote: https://<domain>:<port>/. If you mean this, I doubt it would help because of the remote host.

@brentvatne brentvatne added incomplete issue: missing or invalid repro A minimal reproducible example is required for most issues and removed needs validation Issue needs to be validated labels Feb 22, 2023
@expo-bot
Copy link
Collaborator

Hi there! It looks like your issue requires a minimal reproducible example, but it is invalid or absent. Please prepare such an example and share it in a new issue.

The best way to get attention to your issue is to provide a clean and easy way for a developer to reproduce the issue on their own machine. Please do not provide your entire project, or a project with more code than is necessary to reproduce the issue.

A side benefit of going through the process of narrowing down the minimal amount of code needed to reproduce the issue is that you may get lucky and discover that the bug is due to a mistake in your application code that you can quickly fix on your own.

Resources

Common concerns

"I've only been able to reproduce it in private, proprietary code"

You may not have spent enough time narrowing down the root cause of the issue. Try out the techniques discussed in this manual debugging guide to learn how to isolate the problem from the rest of your codebase.

"I didn't have time to create one"

That's understandable, it can take some time to prepare. We ask that you hold off on filing an issue until you are able to fully complete the required fields in the issue template.

"You can reproduce it by yourself by creating a project and following these steps"

This is useful knowledge, but it's still valuable to have the resulting project that is produced from running the steps, where you have verified you can reproduce the issue.

@vincentdesmares
Copy link

I saw this error while using localhost in development (which works on iOS). Changing this to my machine's static ip (eg. 192.168.0.100) fixes this on Android, although it's a bit annoying when it changes.

I can confirm that switching my Graphql calls from http://localhost:8080 to http://191.168.1.16:8080 solved the issue.

I had only the problem on Android (emulator and phone) but not on IOS and web.

@saibbyweb
Copy link

In my case, the issue was caused by incomplete SSL certificate chain on the web server. Adding the intermediate certificate solved it.

For hosted backends, it definitely has something to do with SSL. As a temporary fix, changing the url prefix to http from https worked for me.

@ZuulAndRibbon
Copy link

In my case, the issue was caused by incomplete SSL certificate chain on the web server. Adding the intermediate certificate solved it.

the same case.Because u don't do this action that .crt and .ca-bundle files have not been combined in one .crt file before uploading to server.You need to do this action before configure ssl in nginx.conf

@linonetwo
Copy link

linonetwo commented Feb 11, 2024

Another case: If your site is using *.crt/*.key SSL pairs, try use *.pem/*.key instead. *.pem file include the necessary root cert for Android devices that does not know your SSL issuer.

@sovetski
Copy link

sovetski commented Feb 17, 2024

I had the same error and my problem was coming from .env file, my keys were not prefixed.

I changed API_URL to EXPO_PUBLIC_API_URL and it worked.

Docs: https://docs.expo.dev/guides/environment-variables/

@adamwoolhether
Copy link

I saw this error while using localhost in development (which works on iOS). Changing this to my machine's static ip (eg. 192.168.0.100) fixes this on Android, although it's a bit annoying when it changes.

This did it for me too, i have to set my backend to listen on the same IP as my macbook, rather than "localhost". Only then can I connect the App with my Android device or simulator.

This isn't an issue with iOS or iOS simulator.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
incomplete issue: missing or invalid repro A minimal reproducible example is required for most issues
Projects
None yet
Development

No branches or pull requests