Angular 7 Http 删除api 句柄正文?
Angular 7 Http delete api handle body?
我正在尝试从 Angular 7 前端与某种 REST API 对话。
要从集合中删除某些项目,除了删除唯一 ID 之外,我还需要发送一些其他数据,即身份验证令牌、一些集合信息和一些辅助数据。
但是,Angular 7 的 Http 模块不太同意带有正文的 DELETE 请求,并尝试发出此请求。
这是我的 api:
DELETE /user/verifications
body {
"doc_type": "govt_id",
"doc_id": "5beedd169db947867b710afd"
}
这适用于 angular 6+,其中 http 是您的 HttpClient
const options = {
headers: new HttpHeaders({
'Content-Type': 'application/json'
}),
body: {
name: 'ravi',
id: 'ravi123'
}
}
this.http.delete('http://localhost:8080/user', options).subscribe(s => {
console.log(s);
})
使用 http.delete
在 angular6 和 angular9 上对我不起作用。我不得不改用 this.http.request('delete', url, options)
。
此外,Typescript 似乎在输入响应方面存在一些问题,因此我不得不使用 map
运算符手动强制输入结果类型:
const httpOptions: any = {
headers: {
'Content-Type': 'application/json'
}
};
const uri = '/path/to/delete/api';
httpOptions.body = {
prop1: value1,
prop2: value2
// ...
};
return this.http.request<string>('delete', uri, httpOptions).pipe(
map((result: any) => result as string), // fix typescript typing
tap((result: string) => {
console.log('response from server:', result);
}
);
我正在尝试从 Angular 7 前端与某种 REST API 对话。
要从集合中删除某些项目,除了删除唯一 ID 之外,我还需要发送一些其他数据,即身份验证令牌、一些集合信息和一些辅助数据。
但是,Angular 7 的 Http 模块不太同意带有正文的 DELETE 请求,并尝试发出此请求。
这是我的 api:
DELETE /user/verifications
body {
"doc_type": "govt_id",
"doc_id": "5beedd169db947867b710afd"
}
这适用于 angular 6+,其中 http 是您的 HttpClient
const options = {
headers: new HttpHeaders({
'Content-Type': 'application/json'
}),
body: {
name: 'ravi',
id: 'ravi123'
}
}
this.http.delete('http://localhost:8080/user', options).subscribe(s => {
console.log(s);
})
使用 http.delete
在 angular6 和 angular9 上对我不起作用。我不得不改用 this.http.request('delete', url, options)
。
此外,Typescript 似乎在输入响应方面存在一些问题,因此我不得不使用 map
运算符手动强制输入结果类型:
const httpOptions: any = {
headers: {
'Content-Type': 'application/json'
}
};
const uri = '/path/to/delete/api';
httpOptions.body = {
prop1: value1,
prop2: value2
// ...
};
return this.http.request<string>('delete', uri, httpOptions).pipe(
map((result: any) => result as string), // fix typescript typing
tap((result: string) => {
console.log('response from server:', result);
}
);