首页 > 解决方案 > 在 Angular 6 中添加 http 标头

问题描述

谁能告诉我这是否是在 Angular 6 中向 http 请求添加标头的正确方法?

当我通过 SwaggerUI 拨打电话时,我可以看到标题应该是:

url -X GET --header 'Accept: application/json' --header 'zumo-api-version: 2.0.0' 'https://myurl/tables/stuff'

所以我添加了以下内容:

let headers: HttpHeaders = new HttpHeaders();
headers = headers.append('HttpHeader1', 'Accept:application/json');
headers = headers.append('HttpHeader2', 'zumo-api-version:2.0.0');

然后调用:

getStuff(){
    return this.http.get('https://myurl/tables/stuff', {headers})
  }

没有失败但没有返回,我知道应该有。

谢谢

更新

刚刚注意到我通话中的网址实际上是 https 而不是 http,这有什么不同吗?

getStuff(){
        return this.https.get('https://myurl/tables/stuff', {headers})
      }

标签: angularhttpvisual-studio-codehttp-headers

解决方案


正确的设置方法headers

let headers: HttpHeaders = new HttpHeaders();
headers = headers.append('Accept', 'application/json');
headers = headers.append('zumo-api-version', '2.0.0');

推荐阅读