angulartypescriptcurlelasticsearch

Elasticsearch curl request not working as expected in Angular 4


I installed the curl npm package (https://www.npmjs.com/package/curl) in my Angular project and I have been using the curl.postJSON() method to post to my Elasticsearch cluster on the cloud. I need to use the curl.get() method now and it isn't working as expected.

In the terminal, this is the curl request that works and I need to use in my Angular project:

curl -XGET https://search-example-xxxxxxxxxxxxxxxxxxx.us-east-2.es.amazonaws.com/myproject/_search? -d '
{ 
    "query" :
    { "match": { "myfield": "NJ-PA-xxxx-x" } }

}
'

This is the method in my main component that isn't working:

myMethod(){
    var getData =  { "query" : { "match": { "myfield": "NJ-PA-xxxx-x" } } };

    var circLink = curl.get("https://search-example-xxxxxxxxxxxxxxxxxxx.us-east-2.es.amazonaws.com/myproject/_search", getData,
      function (err, httpResponse, body) {
          console.log(err, body);
      });
   console.log('this is the circLink: ' + circLink );  
}

This returns "this is the circLink: undefined"

because of the console.log(err, body), it is also returning every document (all 33) in my elasticsearch cluster. Meanwhile when I run the curl command in the terminal it returns only 3 documents. Any ideas how I can get the proper curl request returned?

I think it might have to do with the " -d " in my terminal curl request and not in angular, because when I run the get request without the " -d " in terminal, it returns all of the documents in the elasticsearch cluster. I still don't know how to incorporate the " -d " with curl.get() though


Solution

    1. The curl package has no change since 2011. I recommend the official elasticsearch.js client (https://www.elastic.co/guide/en/elasticsearch/client/javascript-api/current/index.html).

    2. You can send technically a HTTP GET request with a body, but this has downsides for various reasons (GET HTTP request payload)