Angular 2 HTTP Headers not being added to request

我们两清 提交于 2021-02-09 10:56:48

问题


I have the following http request in which I add the header to the request, however the request fails and when I look in the Network tab of the browser the request headers don't include the Authorization header. What am I missing here?:

  public get(address: string, callback: any): Observable<any> {
    let headers: Headers = new Headers();
    headers.append('Authorization', 'Bearer <token>');
    return this.http.get(address, { 'Headers': headers })
      .map(callback)
      .catch((error: any) => this.handleError(error));
  }

Request Headers from the Network tab:

GET /v1/projects HTTP/1.1
Host: <host>
Connection: keep-alive
Pragma: no-cache
Cache-Control: no-cache
Origin: http://localhost:8080
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.143 Safari/537.36
Accept: */*
Referer: http://localhost:8080/
Accept-Encoding: gzip, deflate, sdch
Accept-Language: en-US,en;q=0.8

回答1:


Headers should be headers

this.http.get(address, { headers: headers })


来源:https://stackoverflow.com/questions/40159973/angular-2-http-headers-not-being-added-to-request

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!