6

I'm making a call to an API that gets blob data.

back end sends to me also file name in header.

My actual problem is that I can't get header from the api.

Here's my service.ts

public openFile(path) {
  let url='/download/';
  let pathFile= new HttpParams().set('pathFile', path);
  return this.httpClient.get(url,{params:pathFile, responseType: 'blob' });

and in component.ts I call the service. when I try to print the res.headers I get undefined in console.

openFile(path){
  this.creditPoliciesService.openFile(path).toPromise().then (data => {
    console.log("dataaaaaa",data.headers); // undefined
    var blob = new Blob([data], {type: 'application/pdf'}); 
    if (window.navigator && window.navigator.msSaveOrOpenBlob) {
      window.navigator.msSaveOrOpenBlob(blob);
    }
    else {
      var fileURL = URL.createObjectURL(blob); 
      window.open(fileURL);
    }
  });
}

In the dev tool admin I get informations in response header but I'm not able to find them in the response variable.

4
  • possible duplicate of stackoverflow.com/questions/10548990/… Commented May 2, 2018 at 13:00
  • Reading the full response Commented May 2, 2018 at 13:01
  • Here's i'm using Angular services it's not exactly the same Commented May 2, 2018 at 13:01
  • what's not possible? Commented May 2, 2018 at 13:08

1 Answer 1

9

pass observe key with value of ‘response’ to get the complete response

getData() {
 this.http.get(this.url, { observe: 'response' }).subscribe(res => {
   this.headerProperty = res.headers.get('property name here');
 });
}
Sign up to request clarification or add additional context in comments.

3 Comments

Should I import some modules ?
I added { observe: 'response' } to my request , but I don't get all the header response items that I see in the browser devtool
I have resolved my problem by getting the information from the front end ( instead of the http request ) . but I have found this on github it may help further person github.com/angular/angular/issues/13226

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.