0

I am trying to make elasticSearch Post call within Angular 7 but it is throwing 400 bad error.

public elasticSearch<T>(): Observable<ElasticModel<T>> {
  const reqBody = {
    "query": {
      "bool": {
        "should": {
          "match": {
            "status": "IN_PROGRESS"
          }
        }
      }
    }
  };
  // tslint:disable-next-line:max-line-length
  return this.http.get<ElasticResponseData<T>>(`${this.apiUri}/_search/query?indexName=` + 1 + `&query=` + JSON.stringify(reqBody) + `&size=` + 2 + `&from=` + (page - 1) * size);
  // return this.http.get<ElasticResponseData<T>>(`${this.apiUri}/_search`);
}

I am able to make successful call though when not passing any params and making regular get call

return this.http.get<ElasticResponseData<T>>(`${this.apiUri}/_search`);
meallhour
  • 13,921
  • 21
  • 60
  • 117

1 Answers1

0

The JSON Query you are sending is meant to be sent in the body of the HTTP request. You are sending it through the URL

You have two options:

  1. Use the URI Search DSL found here. This has a more limited set of query options, but it does not require a request body
  2. Modify your Angular code to send the JSON in the actual body of the request. According to this post, HttpClient library does not have an easy way to do this for GET requests. You may have to write custom code for this.
Dennis
  • 415
  • 4
  • 13