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

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

Here is my code:

import { HttpClient, HttpErrorResponse, HttpHeaders } from \'@angular/common/http\';
9条回答
  •  陌清茗
    陌清茗 (楼主)
    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(url, { headers: headers, params: params })
    

提交回复
热议问题