For any URL I try to access from the Emulator using HttpClient (Ionic v3), and I get the message: "Http failure response for (unknown url): 0 Unknown Error

For any URL I try to access from the Emulator using HttpClient (Ionic v3), and I get the message:

            {"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}}

Note: Other apps usually connect to the same emulator
Note: Postman normally connects, Node Js (request) normally connects

Code:

import { HttpClient, HttpHeaders } from '@angular/common/http';
import { Injectable } from '@angular/core';

@Injectable()
export class AuthService {

    constructor(
        private http: HttpClient) {
    }

    loginTest() {

        this.http.get('https://www.google.com/')
            .subscribe(
                ret => {
                    console.log('ret: ', JSON.stringify(ret));
                    alert('ret: ' + JSON.stringify(ret));
                },
                error => {
                    console.log('error: ', JSON.stringify(error));
                    alert('error: ' + JSON.stringify(error));
                }
            );
    }
}
Ionic:

   Ionic CLI          : 5.0.2 (C:\Users\Marcius Bezerra\AppData\Roaming\npm\node_modules\ionic)
   Ionic Framework    : ionic-angular 3.9.5
   @ionic/app-scripts : 3.2.2

Cordova:

   Cordova CLI       : 9.0.0 (cordova-lib@9.0.1)
   Cordova Platforms : android 8.0.0
   Cordova Plugins   : cordova-plugin-ionic-keyboard 2.1.3, cordova-plugin-ionic-webview 4.0.1, (and 6 other plugins)

Utility:

   cordova-res : 0.3.0
   native-run  : 0.2.4

System:

   Android SDK Tools : 26.1.1 (D:\AndroidSdk)
   NodeJS            : v10.14.2 (D:\Program Files\nodejs\node.exe)
   npm               : 6.4.1
   OS                : Windows 10

Same here, im using Ionic 4 and a new project. I’ve setted allow navigation, origin, all CORS headers…

Config.xml

    <content src="index.html" />
    <access origin="*" />
    <allow-navigation href="*" />
    <allow-intent href="http://*/*" />
    <allow-intent href="https://*/*" />
    <allow-intent href="tel:*" />
    <allow-intent href="sms:*" />
    <allow-intent href="mailto:*" />
    <allow-intent href="geo:*" />

Home.page.ts

  ngOnInit() {

    this.request("http://bnb.data.bl.uk/doc/person/ImadaAdriaL%28AdriaLyn%29.rdfjson").subscribe((data: any) => console.log(data));
    this.request("http://aws.random.cat/meow").subscribe((data: any) => console.log(data));
  }

  request(url:string) {

    const headers = { 
      headers: new HttpHeaders({
        "Access-Control-Allow-Origin": "*",
        "Content-Type": "application/json",
        "Access-Control-Allow-Methods": "POST, GET, OPTIONS, DELETE",
        "Access-Control-Allow-Headers": "origin, x-requested-with",
      })
    };

    return this.httpClient.get(url, headers);
  }

Did you ever solve this? I’ve got the same issue in Ionic 5

1 Like

Are you using https endpoints? If not, see if your problem persists even with https everywhere.

Hi :slight_smile: did you find a solution?

@rapropos all my endpoints are HTTPS. @Behra no I haven’t. In order to not burn more time on this I had to keep going, and circle back.

Thanks for your quick response.
I think I resolved it with https://stackoverflow.com/a/51902630/5092978
add this attribute in AndroidManifest.xml under android/app/src/main/ directory

<application android:usesCleartextTraffic="true">
</application>

Hope it helps :slight_smile:

And the error code you’re getting on the client is really 0 like OP? Can you look at the server logs to see if anything is actually reaching the server, or if the client device is “swallowing” the request and never even sending it out?

In Android Studio I’m seeing this:

But, you’re right after looking through the logs the LogMiddleware (which is at the front of the middleware stack) hasn’t inserted any entries for the API being used for the Ionic app.

Also, sorry the local development API is HTTP not HTTPS. When I read your question I was working on setting up the production server with LetsEncrypt.