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

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

Here is my code:

import { HttpClient, HttpErrorResponse, HttpHeaders } from \'@angular/common/http\';
相关标签:
9条回答
  • 2020-11-22 06:17

    The instances of the new HttpHeader class are immutable objects. Invoking class methods will return a new instance as result. So basically, you need to do the following:

    let headers = new HttpHeaders();
    headers = headers.set('Content-Type', 'application/json; charset=utf-8');
    

    or

    const headers = new HttpHeaders({'Content-Type':'application/json; charset=utf-8'});
    

    Update: adding multiple headers

    let headers = new HttpHeaders();
    headers = headers.set('h1', 'v1').set('h2','v2');
    

    or

    const headers = new HttpHeaders({'h1':'v1','h2':'v2'});
    

    Update: accept object map for HttpClient headers & params

    Since 5.0.0-beta.6 is now possible to skip the creation of a HttpHeaders object an directly pass an object map as argument. So now its possible to do the following:

    http.get('someurl',{
       headers: {'header1':'value1','header2':'value2'}
    });
    
    0 讨论(0)
  • 2020-11-22 06:20

    set http headers like below in your http request

    return this.http.get(url, { headers: new HttpHeaders({'Authorization': 'Bearer ' + token})
     });
    
    0 讨论(0)
  • 2020-11-22 06:24

    To add multiples params or headers you can do the following:

    constructor(private _http: HttpClient) {}
    
    //....
    
    const url = `${environment.APP_API}/api/request`;
    
    let headers = new HttpHeaders().set('header1', hvalue1); // create header object
    headers = headers.append('header2', hvalue2); // add a new header, creating a new object
    headers = headers.append('header3', hvalue3); // add another header
    
    let params = new HttpParams().set('param1', value1); // create params object
    params = params.append('param2', value2); // add a new param, creating a new object
    params = params.append('param3', value3); // add another param 
    
    return this._http.get<any[]>(url, { headers: headers, params: params })
    
    0 讨论(0)
  • 2020-11-22 06:24

    In the manual (https://angular.io/guide/http) I read: The HttpHeaders class is immutable, so every set() returns a new instance and applies the changes.

    The following code works for me with angular-4:

     return this.http.get(url, {headers: new HttpHeaders().set('UserEmail', email ) });
    
    0 讨论(0)
  • 2020-11-22 06:24

    In my legacy app Array.from of prototype js was conflicting with angular's Array.from that was causing this problem. I resolved it by saving angular's Array.from version and reassigning it after prototype load.

    0 讨论(0)
  • 2020-11-22 06:25

    I was with Angular 8 and the only thing which worked for me was this:

      getCustomHeaders(): HttpHeaders {
        const headers = new HttpHeaders()
          .set('Content-Type', 'application/json')
          .set('Api-Key', 'xxx');
        return headers;
      }
    
    0 讨论(0)
提交回复
热议问题