Angular 角度HttpClient请求方法如何设置主体
我需要向body发送一个get请求。我正在使用角度HttpClient。我知道get方法不允许发送body,所以我正在尝试使用request方法,但我不知道如何使用它 我能够在没有主体部分的情况下从下面的示例bellow中获取数据,但我确实需要以JSON格式发送主体Angular 角度HttpClient请求方法如何设置主体,angular,angular-httpclient,Angular,Angular Httpclient,我需要向body发送一个get请求。我正在使用角度HttpClient。我知道get方法不允许发送body,所以我正在尝试使用request方法,但我不知道如何使用它 我能够在没有主体部分的情况下从下面的示例bellow中获取数据,但我确实需要以JSON格式发送主体 request(req?: any): any{ const options = createRequestOption(req); return this.http .request<
request(req?: any): any{
const options = createRequestOption(req);
return this.http
.request<ISubscriber[]>("GET", this.resourceUrl,
{
body: '[{"key": "phoneLineType", "operation": ">", "value": "200"}]',
headers: new HttpHeaders({'Content-Type' : 'application/json'}),
params: options,
observe: 'response'
});
}
请求(请求?:任意):任意{
常量选项=createRequestOption(req);
返回此文件。http
.request(“GET”,this.resourceUrl,
{
正文:[{“键”:“电话线类型”,“操作”:“>”,“值”:“200”}],
headers:newhttpheaders({'Content-Type':'application/json'}),
参数:选项,
观察:“回应”
});
}
使用http.get()
只是http.request('get')
的简写。如果您真的需要发送JSON正文,那么您必须使用另一种类型的请求,例如post。您可能需要这样的东西:
return this.http
.post<ISubscriber[]>(
this.resourceUrl,
'[{"key": "phoneLineType", "operation": ">", "value": "200"}]',
{
params: options
{
)
返回this.http
.邮政(
这个.resourceUrl,
“[{”key:“phoneLineType”,“operation:“>”,“value:“200”}]”,
{
参数:选项
{
)
您可能需要更改API端点以期望使用不同的HTTP谓词。我遵循了您的建议,下面是我将来为其他人提供的解决方案
queryPost(body: string, req?: any) : any {
const options = createRequestOption(req);
return this.http.post<ISubscriber[]>(this.searchUrl, body,
{
headers : new HttpHeaders({"Content-Type": "application/json"}),
params: options,
observe: 'response'
});
}
queryPost(body:string,req?:any):any{
常量选项=createRequestOption(req);
返回this.http.post(this.searchUrl,body,
{
标题:新的HttpHeaders({“内容类型”:“应用程序/json”}),
参数:选项,
观察:“回应”
});
}
如前所述,我必须在我的后端应用程序中创建一个新的Post端点
谢谢大家正如您所说,GET请求中没有正文。
request
不会改变这一点:GET
只是request('GET')的缩写
。我想您正在寻找的是一个帖子
请求,请查看感谢您的回复trichetriche。您能告诉我URL是否符合REST标准吗?/api/subscribers/?id=key1,op1,value1&name=key2,op2,value2我想这样我也能解决我的问题