【问题标题】:How to write a request body for elastic search using angular5如何使用 Angular 5 为 elasticsearch 编写请求正文
【发布时间】:2018-02-14 09:12:21
【问题描述】:

我需要从 Angular 触发 REST API。这个 REST API 基本上接受请求正文中的 POST 方法和弹性搜索。

下面的 sn-p 我需要在从 Angular 代码进行休息 API 调用时传递请求正文。

{ "from": 0, "size": 200,
    "query": {
    "bool": { "must": [
    {"match": { "status": "A"}},
    {"match": { "type": "ITEM"}},
    {"terms": {"employee.department": [401,306]}}]
}}}

谁能帮助我如何从角度代码进行此调用?

【问题讨论】:

  • 你尝试过什么?从 Angular 中查找 HttpClient 模块

标签: angular angular5


【解决方案1】:

我已经用 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);
  }

【讨论】:

    猜你喜欢
    • 2015-04-17
    • 2021-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-01
    • 2011-05-12
    • 1970-01-01
    • 2016-08-24
    相关资源
    最近更新 更多