I have an application developed with ionic 5, to manage http calls on the device I use the native http plugin. At startup the application must access the assets folder to retrieve the json with the translation of ngx tralslate. to date I am returning a protocol error from the call. this is the error text: There was an error with the request: no protocol: ./assets/i18n/it-IT.json How do I access the assets folder on the device? What protocol should I enter?
import {Injectable} from “@angular/core”;
import {
HttpErrorResponse,
HttpEvent,
HttpHandler,
HttpInterceptor,
HttpRequest,
HttpResponse
} from “@angular/common/http”;
import {Observable, throwError} from “rxjs”;
import {Platform} from “@ionic/angular”;
import {HTTP} from “@ionic-native/http/ngx”;
import {catchError, finalize, map} from “rxjs/operators”;
import {Store} from “@ngrx/store”;
import {hideLoader, viewLoader} from “…/loader-store/loader.action”;
@Injectable()
export class HttpNativeInterceptor implements HttpInterceptor {
constructor(
private platform: Platform,
private http: HTTP,
private store: Store
) {
}
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
const headers = {};
const params = {};
const {method, url} = req;
this.store.dispatch(viewLoader())
return (this.platform.is('capacitor') ? this.callNative(url, method, headers, params) : next.handle(req))
.pipe(
map((resp: HttpEvent<any>) => {
return resp
}),
catchError((error: HttpErrorResponse) => {
return throwError(error);
}),
finalize(() => {
this.store.dispatch(hideLoader())
})
)
}
callNative(url, method, headers, params) {
return new Observable(ob => {
switch (method) {
case 'GET':
this.http.get(url, headers, params).then(
this.successCallback(ob),
this.errorCallback(ob)
)
break;
case 'POST':
this.http.post(url, headers, params).then(
this.successCallback(ob),
this.errorCallback(ob)
);
break;
case 'PUT':
this.http.put(url, headers, params).then(
this.successCallback(ob),
this.errorCallback(ob)
);
break;
case 'DELETE':
this.http.delete(url, headers, params).then(
this.successCallback(ob),
this.errorCallback(ob)
);
break;
}
});
}
successCallback(ob) {
return (response: any) => {
ob.next(new HttpResponse({body: JSON.parse(response.data)}));
ob.complete();
};
}
errorCallback(ob) {
return (response: any) => {
ob.next(new HttpErrorResponse({error: response.error}));
ob.complete();
};
}
}
[https://stackoverflow.com/questions/63491945/how-to-access-to-assets-directory-with-ionic-native-http](https://stackoverflow.com/questions/63491945/how-to-access-to-assets-directory-with-ionic-native-http)