[英]Overload request headers and params with HttpClient get
在HttpClientModule中,是否有一種方法可以傳遞header和params來獲取請求。
import { HttpHeaders, HttpParams, HttpClient } from @angular/common/http';
const headers = { headers: new HttpHeaders({}) }
let params = new HttpParams({ });
get(url, {params}) // http client get with params
get(url, {headers}); //http client get with headers
我想要一些像requestoptions一樣的東西或者一個語法來做httpClient獲取發送請求頭和參數。
目前正在構建包含搜索參數並發送標題的完整網址。
這是使用get
傳遞頭文件和參數的東西,它使用HttpParamsOptions
將對象序列化為HttpClient可以處理的參數。
localvar: any;
const headers = new HttpHeaders().set('Content-Type', 'application/json');
const myObject: any = { this: 'thisThing', that: 'thatThing', other: 'otherThing'};
const httpParams: HttpParamsOptions = { fromObject: myObject } as HttpParamsOptions;
const options = { params: new HttpParams(httpParams), headers: headers };
this.httpClient.get<any>('https://server:port/api/endpoint', options)
.subscribe((data: any) => {
this.localvar = data;
});
從Angular 5.0.0-beta.6(2017-09-03)開始,您現在可以使用以下語法傳入標題和參數:
const httpOptions = {
headers: { 'Content-Type': 'application/json' },
params: {'include': 'somethingCool'}
};
this.http.get('http://www.example.org', httpOptions);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.