efarley
efarley

Reputation: 8651

Angular 2 HTTP Headers not being added to request

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

Upvotes: 3

Views: 784

Answers (1)

Pankaj Parkar
Pankaj Parkar

Reputation: 136134

Headers should be headers

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

Upvotes: 2

Related Questions