In our Angular5 webapp we call an API to get a zip archive (Backend is ASP.NET Core 2.1 using SharpZipLib). It worked fine using the old HTTP API of Angular :)
But since we upgraded to the new HttpClient API (to handle XSRF using HttpInterceptor), the download now failed without explanations (response of the API is 200 OK)
Here's the actual code:
public getCsvArchive(url: string) {
const headers = new HttpHeaders({ 'responseType': 'arraybuffer' });
return this._http.get(url, { headers } )
.timeout(ServiceConfigurator.TIMEOUT)
.catch(this.handleError);
}
For some reasons if I put directly the "responseType: arraybuffer" into the get method, the download works....but the size of the retrieved file is not what we expected (the archive doesn't even open)
Maybe be the interceptor is the culprit ?
@Injectable()
export class RequestInterceptor implements HttpInterceptor {
private getTokenName = 'XSRF-TOKEN';
private setTokenName = 'X-XSRF-TOKEN';
private tokenValue: string;
constructor(private _cookieService: CookieService) { }
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
let headers = new HttpHeaders({
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*',
'Cache-Control': 'no-cache',
'if-none-match': 'no-match-for-this',
'Pragma': 'no-cache'
});
if (req.method !== 'GET') {
if (this.tokenValue) {
headers = headers.append(this.setTokenName, this.tokenValue);
}
return next.handle(req.clone({ headers }));
} else {
return next.handle(req.clone({ headers })).do((ev: HttpEvent<any>) => {
if (ev instanceof HttpResponse) {
if (environment.use_dp) {
console.log(this._cookieService.get(this.getTokenName));
this.tokenValue = this._cookieService.get(this.getTokenName);
}
this._cookieService.put(this.getTokenName, this.tokenValue);
}
});
}
}
}
blob?