简体   繁体   English

在Angular 6中添加http标头

[英]Adding http headers in Angular 6

Can anyone tell me if this is the correct way to add headers to http requests in Angular 6? 任何人都可以告诉我这是否是在Angular 6中向http请求添加标头的正确方法?

When I make the call via SwaggerUI, I can see the headers should be: 当我通过SwaggerUI进行调用时,我可以看到标题应该是:

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

And then the call: 然后电话:

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. 没有失败,但没有任何回报,我知道应该有。

thanks 谢谢

UPDATE UPDATE

Have just noticed that the url in my call is actually https not http, would that make any difference? 刚刚注意到我的通话中的网址实际上是https而不是http,这会有什么不同吗?

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

The correct way to set headers is 设置headers的正确方法是

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

Angular 6 format: Angular 6格式:

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

The correct format to set the headers would be as shown below. 设置标题的正确格式如下所示。

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

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

In the above request the name of the header keys is Accept & zumo-api-version , the text preceding the : 在上面的请求中,标题键的名称是Acceptzumo-api-version ,前面的文本是
Headers are basically set as key/value pairs 标头基本上设置为键/值对

You're getting nothing in return because you're not subscribing to that event. 你没有得到任何回报,因为你没有订阅那个活动。 add .subcribe to that function where ever you're calling it eg 添加.subcribe到你正在调用它的函数, 例如

getStuff().subscribe(data=>{ console.log(data); } )

so the data you're subscribing to contains all the response and everything you need to know about that call. 所以您订阅的data包含所有响应以及您需要了解的有关该呼叫的所有信息。

You can read more from here https://angular.io/guide/http 您可以在这里阅读更多内容https://angular.io/guide/http

I have done it like this in my code 我在我的代码中这样做了

httpOptions={ headers: new HttpHeaders({ 'Content-Type': 'application/json'})};
 this.httpOptions.headers = this.httpOptions.headers.append('Token', this.Token);

And then in my http.get call, I have done this: 然后在我的http.get调用中,我做了这个:

return this.http.get<JSON>(this.API_ADDRESS+'/api/RemoveEmployee/'+id,this.httpOptions

In angular 6+ 角度为6+

declaration zone: 申报区:

httpOptionsNoAuth : any;

initialization: 初始化:

constructor(){
    this.httpOptionsNoAuth = {
        headers: new HttpHeaders().set('No-Auth', 'true')
    };
}

usage: 用法:

return this._http.get<any>(`${url}`, { headers: this.httpOptionsNoAuth.headers});

尝试以下可能对您有帮助的代码。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM