I have a service like
getOrganizationsFile() {
return this.http.get(`${this.baseUrl}/export`, {
responseType: 'blob',
observe: 'response',
});
}
and I call it in the TS file like
downloadOrganizationsFile() {
this.orgService.getOrganizationsFile().subscribe((res) => {
CommonUtil.saveFile(res)
});
}
and I save the response and download it
static saveFile(res: any) {
const fileName = res.headers.get('content-disposition').split(';')[1].split('=')[1].replace(/\"/g, '');
saveAs(res.body, fileName);
}
My question is in the saveFile method I pass res as parameter, I dont want to use type 'any'.
The response I get from the server is like
is there an default response type in Angular, like 'HttpResponse' so that I can put a type for it instead of using 'any'.
CodePudding user response:
There is a default HttpResponse
imported like this:
import {HttpResponse} from @angular/common/http
Read more about it on -> https://angular.io/api/common/http/HttpResponse
CodePudding user response:
You want to create your own model based on what the server side is returning.
org-file.model.ts
export interface OrgFile {
...
}
getOrganizationsFile(): Observable<OrgFile> {
return this.http.get<OrgFile>(`${this.baseUrl}/export`, {
responseType: 'blob',
observe: 'response',
});
static saveFile(res: OrgFile) {
const fileName = res.headers.get('content-disposition').split(';')[1].split('=')[1].replace(/\"/g, '');
saveAs(res.body, fileName);