Set headers on get request angular 2

前端 未结 2 1397
伪装坚强ぢ
伪装坚强ぢ 2021-02-07 08:27

I try to set an Authorization header to a GET request to authenticate users to a rest API. I\'m using Angular 2 RC1. (I\'am a total beginner).

getTest(){
    let         


        
相关标签:
2条回答
  • 2021-02-07 09:16

    I think that you need the Accept header rather because of the 406 status code...

    let authToken = localStorage.getItem('auth_token');
    let headers = new Headers({ 'Accept': 'application/json' });
    headers.append('Authorization', `Bearer ${authToken}`);
    
    let options = new RequestOptions({ headers: headers });
    return this._http
      .get(this._url,options)
      .map(res => console.log(res));
    

    This allows you to tell the server which content type you expect in the response...

    The Content-Type header is rather to specify the type of the content you sent in the request. In your case, there is no content...

    0 讨论(0)
  • 2021-02-07 09:17
    autorization =  { Authorization: 'Token adfasdfadf651f65asd1f65asdf' }
    
    this.http.get(url, { headers: autorization})
    
    0 讨论(0)
提交回复
热议问题