Search code examples
rxjsangular7angular-httpclient

How to get header properties from backend api using rxjs?


I am getting a file stream from backend. the header keeps the name of the file with extension. But how to get those property in fronent end. here is my code,not getting the value as well no error.

downloadFile(id:number):Observable<any> {

        const options = { responseType: 'blob' as 'json' }

        return this.http.get<any>(environment.baseUrl+`CourseFileUpload/${id}`, options)
        .pipe(
            map((file) => {
                console.log('header', file.headers('Content-Disposition')); //not getting header value...!?
                return new Blob([file], {type: "application/octet-stream"})
            }),
            catchError(this.handleError)
        )
    }

any one help me?

I tried with suggestion like :

downloadFile(id:number):Observable<any> {

        const headers = new HttpHeaders({ observe: 'response'});
        const options = { responseType: 'blob' as 'json', headers:headers  }

        return this.http.get<any>(environment.baseUrl+`CourseFileUpload/${id}`,  options )
        .pipe(
            map(resp => {
                if(resp.headers){
                    const keys = resp.headers.keys();
                    console.log('file',  keys); //nothing consoles!?
                }

                return new Blob([resp], {type: "application/octet-stream"})
            }),
            catchError(this.handleError)
        )
    }

got no response. please any one help me to get response header?


Solution

  • From the documentation you need to add observe: 'response' into the options to have access to the full response object.