I am using HttpInterceptor
and I have an Http service that calls http methods running HttpClient. I am trying to get the progress of the upload and I am facing two issues here,
First, progress event is only being caught by HttpInterceptor
and not any of my Http caller methods in my service. it looks like the former is masking the progress reporting.
Second, The progress value always starts with 100%, and the it starts the increment.
I am lazily loading my module, HttpInterceptor is registered at the app.module level.
How can I get the progress value from an http method?
my HttpInterceptor
service looks like,
if (req.url.search('https') < 0) {
const headers = new HttpHeaders({
Accept: 'application/json',
Authorization: `Bearer ${this.authService.getToken()}`,
'X-XSRF-TOKEN': `${this.authService.getAntiforgeryToken()}`,
ClientTimeZoneOffest: `${new Date().getTimezoneOffset()}`,
ClientTimeZoneId: Intl.DateTimeFormat().resolvedOptions().timeZone,
ClinetLogInId: `${this.authService.getLogInId()}`,
});
const cloneReq = req.clone({ headers });
return next.handle(cloneReq).pipe(
mergeMap((ev: HttpEvent<any>) => {
if (ev.type === HttpEventType.UploadProgress) {
const percentDone = Math.round((100 * ev.loaded) / ev.total);
console.log(`File is ${percentDone}% uploaded.`);
}
this.httpResponseHandlerService.handleSuccessResponse(ev);
return Observable.of(ev);
}),
catchError(error => {
this.httpResponseHandlerService.handleErrorResponse(error, req);
return Observable.throw(error);
}),
);
} else {
return next.handle(req);
}
}
my Http caller service,
public upload<T>(apiUrl: string, jsonData: {} = {}) {
return this.httpService.post<T>(this.baseUrl + apiUrl, jsonData, {
reportProgress: true,
});
}
and the method where I am trying to get the progress in is like,
this.httpService
.upload(this.api + this.id.value, data)
.takeUntil(this.unsubscribe)
.subscribe((ev: HttpEvent<any>) => { // Nothing is happening here!
if (ev.type === HttpEventType.UploadProgress) {
const percentDone = Math.round((100 * ev.loaded) / ev.total);
console.log(`File is ${percentDone}% uploaded.`);
}
});
The progress behavior is,