GET request with body

Hello,

so I am making a request to elasticSearch and I want to filter the data received by Timestamp value, I want every data between 6 and 8 A.M. for example. In postman the Body -> raw is something like this:

``{
"query": { 
    "bool": { 
      "filter": [ 
        { "range": { "timestamp": { "gte": "2019-08- 09T06:00:00","lt":"2019-08-09T07:00:00"}}} 
      ]
    }
  }
}
``

In kotlin I am trying the next piece of code but it doesn’t work. What do I have to change in order to work out? Thanks!

        val forEntity_Flow = restTemplate.exchange("https://X.X.X.X:6200/bla_bla-$da/_search/?size=50&pretty=1",
            HttpMethod.GET, HttpEntity("{\"query\":{\"bool\":{\"filter\":[{\"range\":{\"timestamp\":{\"gte\":\"2019-08-09T06:00:00\",\"lt\":\"2019-08-09T08:00:00\"}}}]}}}", headers), ResultsFlow::class.java)

Copy pasting the same question after an hour doesn’t get you more replies (Also having to wait for an hour is not that uncommon. Depending on how many people have the expertise to help you, you sometimes need to wait a few days.)

I have no idea about elasticSearch, their api or whether your query is correct so I can’t help you with that. If your problem is a kotlin error you should post the error message. If the error is related to elasticSearch you should firstly explain what you get for an result, what you expect but also maybe try to look for a elasticSearch forum or some place with people who use it so they can help you.

1 Like

It’s the first time I use the forum, wanted to spread to at least two topics I am sorry for the abuse, won’t happen again.
It doesn’t give any error message, the querry that is supposed to filter the data just doesn’t do anything in the other hand in a postman request that querry does work.

Thanks for the information about the waiting time for a reply, wasn’t aware of it.

A sáb, 10/08/2019, 00:52, Burkhard Mittelbach via Kotlin Discussions kotlinlang@discoursemail.com escreveu:

The server-side meaning of GET in the HTTP spec says that the body never had any meaning. See https://stackoverflow.com/questions/978061/http-get-with-request-body for example.

That may be why the Kotlin client doesn’t send the body you’re providing. Usually a REST API uses POST to include parameters in the body, even if you’re doing a GET-like thing; or uses GET with parameters in the query string. Are you sure the ElasticSearch API doesn’t specify POST for this? If it does specify GET with a request body, it’s a bad, non-standard API!

I am using Spring MVC as the support for the application and as I searched I found that Spring doesn’t support a body in a GET request. I tried to make the same request but changed the request to POST and it changed some indexes of elasticsearch used for testing.
Don’t really know what I should use now

A sáb, 10/08/2019, 10:27, Hugh Greene via Kotlin Discussions kotlinlang@discoursemail.com escreveu:

ElasticSearch likes to use GET request for all read-only operations, including searching. Since ElasticSearch is mainly a search engine, and a fairly complex one at that, search requests are often too complex to be expressed without JSON request body. As a result, ElasticSearch makes heavy use of request bodies in GET request. It’s not part of the HTTP standard, but that is how they do it.

My recommendation is, if possible, use an ElasticSearch client library rather than a generic HTTP client. If you are developing for the JVM, then there is the client for Java, as well as some community created adapters for Kotlin.