7

I am working on ionic 4 using capacitor. I tried to call an api from localhost but am getting this error

There was an error with the request: Cleartext HTTP traffic to localhost not permitted!!

1 Answer 1

15

Since android 9 they try to push people to use https APIs instead of http. But there are ways to disable it.

The simple answer is to allow http in your whole app by modifying the AndroidManifest.xml and adding android:usesCleartextTraffic="true" to the application

<application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:usesCleartextTraffic="true"

For a more fine tuned domain dependent approach I recommend reading this: https://developer.android.com/training/articles/security-config#CleartextTrafficPermitted

3
  • Hello tobika, Thanks for your reply. I've modified AndroidManifest.xml and i got another error message : There was an error with the request: Failed to connect to localhost/127.0.0.1:80 Do you have any idea how to solve this? Thanks in advance
    – user5946061
    Commented Feb 9, 2020 at 14:27
  • 1
    maybe you should start another question? just to be sure (because maybe I missunderstood), you are also calling the api that is running on localhost? localhost in the android app is the telephone and not your computer, just in case the problem is there
    – tobika
    Commented Feb 10, 2020 at 16:41
  • @user5946061 I'm pretty sure you can find your answer here: https://stackoverflow.com/questions/5806220/how-to-connect-to-my-http-localhost-web-server-from-android-emulator - try 10.0.2.2 instead of localhost.
    – Glorfindel
    Commented Jul 2 at 16:30