capacitor: Error response external api
Good evening, im developing an app with an external API. In web, ionic dev app and npx cap serve, all works fine, but when i try to package the app with capacitor and deploy in android studio, i have this error with all requests:
Login Error: {"headers":{"normalizedNames":{},"lazyUpdate":null,"headers":{}},"status":0,"statusText":"Unknown Error","url":null,"ok":false,"name":"HttpErrorResponse","message":"Http failure response for (unknown url): 0 Unknown Error","error":{"isTrusted":true}}
Does anyone know why?
Regards.
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Reactions: 8
- Comments: 22 (7 by maintainers)
You can set
usesCleartextTraffic="true"only for debug build like this AndroidManifest.xmlbuild.gradle
because by default the current android version blocks access to http. however as i now learned this can be disabled via
android:usesCleartextTraffic="true", which i did now.Got the problem! Chrome by default doesn’t allow mixed content (ie fetching HTTP from an HTTPS server), and Capacitor serves the app using HTTPS. There’s a configuration in capacitor.config.json that overrides this and allows mixed content fetching, just add this to the file and it’s gonna work:
(well, at least that was MY problem, because my API is served with HTTP. If that’s the case for you, this is how you fix it.)
I have all cors enabled. My API is in laravel, i use the same post method on Ionic 3 with the same API and It works. This only have with capacitor, not when i compile the app in Cordova. Seems a capacitor problem…
El sáb., 27 oct. 2018 23:15, ydorea notifications@github.com escribió:
iI can confirm @Raerten answer works where the mixed content solution did not. Thank you! Is there any capacitor-friendly way of setting this flag through capacitor config?
for me it was a problem with a self signed certificate for my local dev server. apparently it’s not enough for android to install the cert. patching capacitors
Bridge.javato accept my cert solved it for me, see https://stackoverflow.com/a/43594572 - waiting for a better solution thoughI added allowMixedContent: true to the capacitor.config.json, and it doesn’t help. I got the error
Using below: “@capacitor/android”: “^1.1.0”, “@capacitor/core”: “1.1.0”,
I ran “npx cap sync” after added the allowMixedContent.
Am I missing anything?
You could also try setting “Access-Control-Allow-Headers” to a specific value instead of using the wildcard (‘*’).
The wildcard might cause issues as support for it is not fully implemented, yet (see https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Allow-Headers#Compatibility_notes).
So for the Spring config above you could try something like this: