Http requests not working in ionic app when deployed to device

Viewed 843

I am working on ionic app which is supposed to make http request to an api. This means I cannot change server side settings. I am using the angular http module. My first attempt gave rise to a CORS error, I managed to fix by using a proxy like below:

"proxies": [
    {
      "path": "/api",
      "proxyUrl": "https://api.com/api/"
    }
  ]

This works perfectly fine in the browser when using ionic serve, however when deploying to a device the request does not seem to work.

When using the browser it give a status code 200 with a proper response. Response code

When deployed it seems looks like it returns the same response, however in the console the following error is displayed (this is retrieved using the remote devices in google chrome), also the response tab for the request is empty.

Edit: I checked the AndroidManifest.xml file and it contains the internet permission. However I tried to build it to android with the full URL instead of the proxy URL, but then the app unexpectedly closes.

1 Answers

So you're working directly with 3rd party API? Usually it's considered as a bad practice, because you have to store or retrieve your API keys on clientside app. I suggest you to implement your own backend and proxy requests to the API. If there is no keys, or you simply don't care about security you could try other solutions, here is a great article about CORS: https://ionicframework.com/docs/faq/cors

Related