Native HTTP and local access


I’m using the Ionic native HTTP plugin, which is working fine. However, at least during development and testing, we sometimes need to load a local JSON file, located in the assets folder.

Is it possible to access this file with the native HTTP plugin? Or is there a better way, other than using the Angular HttpClient as fallback?

Thanks in advance,

While you wait for better answers, I’d like to make the case for Angular HttpClient as the primary interface to the rest of your app code. It makes for much less, easier-to-read, and type-safe client code. It’s way easier to debug. It works everywhere. It has interceptors, which are key to the rest of this scenario.

If there is some specific request that absolutely needs the cordova HTTP client, why not do that from within an Angular HttpClient interceptor?

Using the native plugin eliminates the need for CORS configuration on our REST backend.

That’s typically the reason people give. I personally don’t find it a compelling reason, but that’s beside the point here.

What I’m suggesting is that you do all those shenanigans from within an Angular HttpClient interceptor, so all the rest of your app talks to Angular’s HttpClient. Inside that interceptor, you can look at the URL or for the presence of a special header or whatever, and if the situation warrants it, have your interceptor pass the request off to the native HTTP plugin and clothe its response to look like one that would have come from HttpClient.

Basically, instead of native HTTP with an Angular HttpClient fallback, I’m suggesting Angular HttpClient with an (undetectable to consumers) native HTTP fallback.

Of course, that could be a solution.

However, we already had an Angular service, which acts as kind of gateway to our REST backend. So the client code only talks to this service, which also does some kind of type checking, validation and such. Depending of the platform, this service uses either the native plugin or the Angular HttpClient.

The only dirty things are these local config files we use during testing…

Regarding CORS: Our REST backend is running on customer systems, so we do not have full control over the configuration and have to explain to them everything. So we want to limit the number of “unusual” things.

Then if you can identify requests for the local config files, I would stick them in the “let Angular handle them” bucket. I don’t believe there is any way for native HTTP to do this, because it requires collaboration from the webview. Apologies if you’re already aware, but “the assets folder” is a lie. It’s really served from inside your app binary.

True, but I think, everyone known what I was talking about :wink: