Search code examples
httpionic-frameworkcapacitor

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


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!!


Solution

  • 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