# Axios + HTTP request not working on a physical device

**URL:** <https://forum.ionicframework.com/t/axios-http-request-not-working-on-a-physical-device/194813>\
**Category:** Ionic React\
**Created:** [August 20, 2020, 6:56pm UTC](https://forum.ionicframework.com/t/axios-http-request-not-working-on-a-physical-device/194813 "2020-08-20T18:56:16Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![serjuice](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/serjuice/32/133591_2.png) [@serjuice](https://forum.ionicframework.com/u/serjuice)\
**Post date:** [August 20, 2020, 6:56pm UTC](https://forum.ionicframework.com/t/axios-http-request-not-working-on-a-physical-device/194813/1 "2020-08-20T18:56:16Z")

</div>

I’m fetching data using axios and external API (non-https), everything is working fine with web version, postman, even with my phone’s browser.

But I can’t get it working on my actual device. Android Studio is telling me there is “an network error”, the request is sent to the server but the server didn’t respond. That’s all.

I configured CORS following the official Ionic documentation. I’ve set all needed origins and options (can’t use \* origin because I have to use credentials with my requests). Also tried without Nginx, without credentials/cookies, tried using ‘\*’ origin but the error reoccur.

Server:

```auto
const allowedOrigins = [
  'capacitor://localhost',
  'ionic://localhost',
  'http://localhost',
  'http://localhost:8080',
  'http://localhost:8100'
];

// Reflect the origin if it's in the allowed list or not defined (cURL, Postman, etc.)
const corsOptions = {
  origin: (origin, callback) => {
    if (allowedOrigins.includes(origin) || !origin) {
      callback(null, true);
    } else {
      callback(new Error('Origin not allowed by CORS'));
    }
  },
  credentials: true,
  optionsSuccessStatus: 200,
  exposedHeaders: ["set-cookie"]
}

// Enable preflight requests for all routes
app.options('*', cors(corsOptions));

app.use(cors(corsOptions));

```

Client:

```auto
try {
  const response = await axios('http://my.api.url', {
    method: 'get',
    withCredentials: true,
      headers: {
        'Accept': 'application/json'
      }
    });
  
  const data = response.data;
      
  // Do something with the data
      
} catch(error) {
  console.log(error);       
};

```

The main problem is that I’m not even sure if this is a http(s) or CORS related error.

---

<div class="post-metadata">

**Author:** ![richardshergold](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/richardshergold/32/91823_2.png) [@richardshergold](https://forum.ionicframework.com/u/richardshergold)\
**Post date:** [August 20, 2020, 8:13pm UTC](https://forum.ionicframework.com/t/axios-http-request-not-working-on-a-physical-device/194813/2 "2020-08-20T20:13:50Z")

</div>

Does the server return anything or do you get any errors? I’m having problems with Android today and when the call is made nothing happens. No Error, No response. Nothing. I’ve posted in the forum about it today.

---

<div class="post-metadata">

**Author:** ![serjuice](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/serjuice/32/133591_2.png) [@serjuice](https://forum.ionicframework.com/u/serjuice)\
**Post date:** [August 20, 2020, 8:34pm UTC](https://forum.ionicframework.com/t/axios-http-request-not-working-on-a-physical-device/194813/3 "2020-08-20T20:34:02Z")

</div>

No response from the server, too. I send the request and after that I catch an error as you can see in that try-catch block.

I saw your post a bit too late, just after I made this post. I think that this has something to do with HTTPS (I don’t know if your API has SSL or not). CORS configuration is the same as in documentation.

What’s your configuration?

---

<div class="post-metadata">

**Author:** ![richardshergold](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/richardshergold/32/91823_2.png) [@richardshergold](https://forum.ionicframework.com/u/richardshergold)\
**Post date:** [August 20, 2020, 8:46pm UTC](https://forum.ionicframework.com/t/axios-http-request-not-working-on-a-physical-device/194813/4 "2020-08-20T20:46:02Z")

</div>

I’m not involved at the server end but I don’t think this is a CORS issue as we don’t see any CORS errors. Our app uses this to get round CORS:

> **[ionic-native-http-connection-backend](https://www.npmjs.com/package/ionic-native-http-connection-backend)**
>
> A solution to CORS problem with Ionic and WKWebView

That’s been in our Ionic 3 app for the last 18 months and I’ve left it in place in the Ionic 5 app.

---

<div class="post-metadata">

**Author:** ![serjuice](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/serjuice/32/133591_2.png) [@serjuice](https://forum.ionicframework.com/u/serjuice)\
**Post date:** [August 20, 2020, 8:54pm UTC](https://forum.ionicframework.com/t/axios-http-request-not-working-on-a-physical-device/194813/5 "2020-08-20T20:54:53Z")

</div>

Weird thing is that I just used a generic HTTPS API I found on web and I get the same behaviour (like with my non-https API). Web app works, postman works, android 10 app on physical device does not.

---

<div class="post-metadata">

**Author:** ![richardshergold](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/richardshergold/32/91823_2.png) [@richardshergold](https://forum.ionicframework.com/u/richardshergold)\
**Post date:** [August 20, 2020, 8:58pm UTC](https://forum.ionicframework.com/t/axios-http-request-not-working-on-a-physical-device/194813/6 "2020-08-20T20:58:42Z")

</div>

I’ve just tested out my Ionic 3 app on the same Android 10 device pointing at the same server and it works fine. So it’s something in my project that’s making it go wrong even though I have removed and re-added the Android platform.

---

<div class="post-metadata">

**Author:** ![serjuice](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/serjuice/32/133591_2.png) [@serjuice](https://forum.ionicframework.com/u/serjuice)\
**Post date:** [August 20, 2020, 9:10pm UTC](https://forum.ionicframework.com/t/axios-http-request-not-working-on-a-physical-device/194813/7 "2020-08-20T21:10:12Z")

</div>

I think I fixed it.

Those are the lines I added to my capacitor.config.js (don’t know which one actually fixed, but requests are working now). Now going to bed.

```auto
"server": {
    "androidScheme": "http",
    "allowNavigation": [
      "my.api.url"
    ]
  }

```

[Capacitor Configuration - Documentation](https://capacitorjs.com/docs/reference/config)

Try and see if it works for you.

Update: I removed “androidScheme”: “http” and left “allowNavigation” field and looks like everything’s fine now. I’ll see tomorrow if the problem is really solved.

Update 2: Important thing is that it still does not work if I try that using non-https API, I had to use https API to get a response from server. But at least I know where is the problem.

---

<div class="post-metadata">

**Author:** ![richardshergold](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/richardshergold/32/91823_2.png) [@richardshergold](https://forum.ionicframework.com/u/richardshergold)\
**Post date:** [August 20, 2020, 9:13pm UTC](https://forum.ionicframework.com/t/axios-http-request-not-working-on-a-physical-device/194813/8 "2020-08-20T21:13:05Z")

</div>

I will try first thing tomorrow. Glad you got yours sorted!

---

<div class="post-metadata">

**Author:** ![richardshergold](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/richardshergold/32/91823_2.png) [@richardshergold](https://forum.ionicframework.com/u/richardshergold)\
**Post date:** [August 21, 2020, 5:44am UTC](https://forum.ionicframework.com/t/axios-http-request-not-working-on-a-physical-device/194813/9 "2020-08-21T05:44:03Z")

</div>

Thanks for the suggestion but adding that made no difference to my app @serjuice ☹

---

<div class="post-metadata">

**Author:** ![serjuice](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/serjuice/32/133591_2.png) [@serjuice](https://forum.ionicframework.com/u/serjuice)\
**Post date:** [August 21, 2020, 7:01am UTC](https://forum.ionicframework.com/t/axios-http-request-not-working-on-a-physical-device/194813/10 "2020-08-21T07:01:22Z")

</div>

Did you try to send a request to another API? I found one on the internet and everything is working properly after adding that one line, I still have to see if everything is working with my own server but since I didn’t set up SSL on my web server I can’t be sure.

Update: Added SSL to my webserver and now it works too. allowNavigation definitely sorted it out for me.

---

<div class="post-metadata">

**Author:** ![richardshergold](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/richardshergold/32/91823_2.png) [@richardshergold](https://forum.ionicframework.com/u/richardshergold)\
**Post date:** [August 25, 2020, 3:30pm UTC](https://forum.ionicframework.com/t/axios-http-request-not-working-on-a-physical-device/194813/11 "2020-08-25T15:30:20Z")

</div>

All working ok for us now. Because we are in control of the server I was able to talk our administrator into allowing the requests in (we specify the two device origins in the capacitor config). Secondly, because we need to use htttp-interceptors we are sticking with the Angular HttpClient (I love http-interceptors so not being able to use them is a major drawback for me of using anything but the HttpClient). The nice thing about all this is that we are now able to remove two dependencies from the app (the advanced-http-plugin and the ionic-native-http-backend-plugin) and anything that reduces dependencies is a good thing!

---

<div class="post-metadata">

**Author:** ![AbdulAzeez](https://avatars.discourse-cdn.com/v4/letter/a/ec9cab/32.png) [@AbdulAzeez](https://forum.ionicframework.com/u/AbdulAzeez)\
**Post date:** [April 8, 2022, 2:13pm UTC](https://forum.ionicframework.com/t/axios-http-request-not-working-on-a-physical-device/194813/12 "2022-04-08T14:13:02Z")

</div>

Hi, the above solution did not work for me… Any suggestions

---

<div class="post-metadata">

**Author:** ![OrganGrindingMonkey](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/organgrindingmonkey/32/131678_2.png) [@OrganGrindingMonkey](https://forum.ionicframework.com/u/OrganGrindingMonkey)\
**Post date:** [April 20, 2022, 9:55pm UTC](https://forum.ionicframework.com/t/axios-http-request-not-working-on-a-physical-device/194813/13 "2022-04-20T21:55:44Z")

</div>

Are you using HTTPS?

The newer versions of Android make it required to use HTTPS, there are some workarounds but I haven’t tried them.

It would be helpful if you can provide some info on your codebase and what you’ve tried to fix the issue.

---

<div class="post-metadata">

**Author:** ![AbdulAzeez](https://avatars.discourse-cdn.com/v4/letter/a/ec9cab/32.png) [@AbdulAzeez](https://forum.ionicframework.com/u/AbdulAzeez)\
**Post date:** [May 6, 2022, 10:17pm UTC](https://forum.ionicframework.com/t/axios-http-request-not-working-on-a-physical-device/194813/14 "2022-05-06T22:17:58Z")

</div>

Already fixed. Thanks

---

<div class="post-metadata">

**Author:** ![claudioasv](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/claudioasv/32/141483_2.png) [@claudioasv](https://forum.ionicframework.com/u/claudioasv)\
**Post date:** [May 9, 2022, 4:33pm UTC](https://forum.ionicframework.com/t/axios-http-request-not-working-on-a-physical-device/194813/15 "2022-05-09T16:33:40Z")

</div>

Hola! ¿podrías publicar lo que hiciste para arreglarlo? te lo agradecería ya que tengo el mismo error.

---

<div class="post-metadata">

**Author:** ![lassoxo](https://avatars.discourse-cdn.com/v4/letter/l/e495f1/32.png) [@lassoxo](https://forum.ionicframework.com/u/lassoxo)\
**Post date:** [May 16, 2022, 7:11pm UTC](https://forum.ionicframework.com/t/axios-http-request-not-working-on-a-physical-device/194813/16 "2022-05-16T19:11:33Z")

</div>

I use Axios for the PWA.

I use HTTP for mobile, since native plugins might be better suited for networking across a plethora of physical devices:

> **[HTTP | Ionic Documentation](https://ionicframework.com/docs/native/http)**
>
> HTTP Server Plugin Documentation: Advantages for iOS and Android

---

<div class="post-metadata">

**Author:** ![irwin7](https://avatars.discourse-cdn.com/v4/letter/i/b9bd4f/32.png) [@irwin7](https://forum.ionicframework.com/u/irwin7)\
**Post date:** [September 20, 2023, 10:59am UTC](https://forum.ionicframework.com/t/axios-http-request-not-working-on-a-physical-device/194813/17 "2023-09-20T10:59:32Z")

</div>

thank you it worked for me
