我要求从Angular触发REST API。这个rest API基本上接受请求体中的POST方法和弹性搜索。
我需要在从Angular代码进行休息API调用时传递请求正文的以下代码段。
{ "from": 0, "size": 200,
"query": {
"bool": { "must": [
{"match": { "status": "A"}},
{"match": { "type": "ITEM"}},
{"terms": {"employee.department": [401,306]}}]
}}}
有人可以帮我解决如何使用角度代码进行此调用吗?
答案 0 :(得分:0)
我已经尝试过Http模块,它正在工作。
const requestBody = { "from": 0, "size": 200,
"query": {
"bool": { "must": [
{"match": { "status": "A"}},
{"match": { "type": "dept"}},
{"terms": {"employee.department": [id1, id2]}}]
}}};
var headers = new Headers();
headers.append('Content-Type', 'application/json');
headers.append('Accept', 'application/json');
headers.append('Authorization', 'Basic <token>');
const url = 'https://<rest-api>.com/<service>/v1/departments/@search' + '?apikey=' + '<<apikey>>';
return this.httpHelperService.post(url, requestBody, {headers: headers});
post(url: string, data: any, args?: RequestOptionsArgs): Observable<any> {
if (args == null) {
args = {};
}
if (args.headers === undefined) {
this.getHeaders();
args.headers = this.headers;
}
return this._http.post(url, JSON.stringify(data), args)
.map((res: Response) => HttpHelperService.json(res))
.catch(this.handleError);
}