Adding a HTTP header to the Angular HttpClient doesn't send the header, why?

前端 未结 9 1081
孤街浪徒
孤街浪徒 2020-11-22 05:45

Here is my code:

import { HttpClient, HttpErrorResponse, HttpHeaders } from \'@angular/common/http\';
9条回答
  •  不知归路
    2020-11-22 06:40

    First, you need to add HttpHeaders with HttpClient

    import { HttpClient,HttpHeaders  } from '@angular/common/http';
    

    your constructor should be like this.

    constructor(private http: HttpClient) { }
    

    then you can use like this

       let header = new HttpHeaders({ "Authorization": "Bearer "+token});
    
       const requestOptions = {  headers: header};                                                                                                                                                                            
    
        return this.http.get(url, requestOptions)
            .toPromise()
            .then(data=> {
                //...
                return data;
        });
    

提交回复
热议问题