使用HttpClient重载请求标头和参数 [英] Overload request headers and params with HttpClient get
本文介绍了使用HttpClient重载请求标头和参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在HttpClientModule中,有一种方法可以传递标头和参数来获取请求.
In HttpClientModule, is there a method to pass headers and params to get request.
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进行发送请求标头和参数的语法.
I want something like requestoptions to hold both or a syntax to do httpClient get sending request headers and params.
当前使用搜索参数构建完整的url并一起发送标头.
Currently building complete url with search params and sending headers along.
推荐答案
此处使用get
传递标头和参数,并使用HttpParamsOptions
将对象序列化为HttpClient可以处理的参数
Here is something that passes both headers and parameters in with a get
, and it uses HttpParamsOptions
to serialize an object into parameters that the HttpClient can cope with.
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;
});
这篇关于使用HttpClient重载请求标头和参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文