1

I'm trying to perform a GET request to elastic search api which is needed in this form

GET /_search
{
    "query": {
        "more_like_this" : {
            "fields" : ["title", "description"],
            "like" : "Once upon a time",
            "min_term_freq" : 1,
            "max_query_terms" : 12
        }
    }
}

I used request But I can't find how to add body to the request.

Any help?

LucasSeveryn
  • 5,252
  • 8
  • 32
  • 58
abdoutelb
  • 965
  • 1
  • 12
  • 32

4 Answers4

3

You can see the document about request(options, callback)

Also, GET method should't send any body, please confirm it's not POST.

request.get('http://localhost:8092/_search', {
  json: true,
  body: {
    "query": {
        "more_like_this" : {
            "fields" : ["title", "description"],
            "like" : "Once upon a time",
            "min_term_freq" : 1,
            "max_query_terms" : 12
        }
    }
  }
})
joaner
  • 636
  • 1
  • 5
  • 17
  • API is really GET method, it's works but weird. https://www.elastic.co/guide/en/elasticsearch/reference/6.3/_the_search_api.html – joaner Jun 25 '18 at 10:27
  • yes the GET request not recommended with body but it's happened :D BTW i was missing the `json: true,` – abdoutelb Jun 25 '18 at 10:34
  • More about `GET` with body at this thread: https://stackoverflow.com/questions/978061/http-get-with-request-body – t3__rry Jun 25 '18 at 11:56
1

If you use GET, you can't have body, you just have query. You can convert your query to string and add to your url, or use option with qs:

option = {
    url: 'your_url',
    qs: your_query
};
request(option, (error,res)=>{});

If want use body, you should use POST.

hong4rc
  • 3,334
  • 2
  • 16
  • 36
-1

Hi have you tried simply passing your body as a second argument to your request?

request.get('yourEndpoint', { // your body })
t3__rry
  • 2,536
  • 1
  • 19
  • 33
-1

You can not add body to a get request, you will have to add a query string for the request to send those data.