从 Angular 中的获取 Http 请求中获取标头 [英] get headers from get Http request in Angular

查看:80
本文介绍了从 Angular 中的获取 Http 请求中获取标头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在调用获取 blob 数据的 API.

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

后端还向我发送标题中的文件名.

back end sends to me also file name in header.

我的实际问题是我无法从 api 获取标题.

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

这是我的service.ts

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

并在 component.ts 中调用该服务.当我尝试打印 res.headers 我在控制台中未定义.

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.

推荐答案

传递值为'response'的观察键以获得完整的响应

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');

 });
}

这篇关于从 Angular 中的获取 Http 请求中获取标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆