本文介绍了在Angular 6中添加http标头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Can anyone tell me if this is the correct way to add headers to http requests in Angular 6?

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

When I make the call via SwaggerUI, I can see the headers should be:

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

所以我添加了以下内容:

so I have added the following:

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})
  }

没有失败,但没有任何回报,我知道应该有.

There is no failure but nothing is returned, and I know that there should be.

谢谢

更新

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

Have just noticed that the url in my call is actually https not http, would that make any difference?

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

推荐答案

设置 headers

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

这篇关于在Angular 6中添加http标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 17:18