I would like to know why this code runs but doesn't filter the data as it should.
The same request in postman works but in Kotlin it doesn't what is happening?
The goal is to filter the data by timestamp value.
val getFiltered = restTemplate.exchange(
"https://X.X.X.X:6200/ble_flow-$da/_search/?size=50&pretty=1",
HttpMethod.GET, HttpEntity("{\\r\\n\\\"query\\\": { \\r\\n \\\"bool\\\": { \\r\\n \\\"filter\\\": [ \\r\\n { \\\"range\\\": { \\\"timestamp\\\": { \\\"gte\\\": \\\"2019-08-12T06:00:00\\\",\\\"lt\\\":\\\"2019-08-12T011:00:00\\\"}}} \\r\\n ]\\r\\n }\\r\\n }\\r\\n}", headers),
ResultsFlow::class.java)
println(getFiltered)
It would solve the problem if I could transform the body:
{
"query": {
"bool": {
"filter": [
{ "range": { "timestamp": { "gte": "2019-08-12T06:00:00","lt":"2019-08-12T07:00:00"}}}
]
}
}
}
into url query. But I don' really know how to do this.
Thanks.
The Spring RestTemplate does not send your Body in a GET request, as a GET request should not contain a body but use query parameters instead. Read more on it here HTTP GET with request body
Therefore the Elasticsearch API allows also POST to send a query with a body. I would recommend this as your first solution:
https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-body.html
Both HTTP GET and HTTP POST can be used to execute search with body. Since not all clients support GET with body, POST is allowed as well.
If you really want to use a GET request from Spring RestTemplate to transfer a body you need to replace and extend the RequestFactory. You can find an example for exactly your case in this blog post.
Related
I am integrating my app with a third party app and there is a requirement where I have to call their APIs. So, there is this GET call which when supplied with proper headers and params and subsequently called, returns some JSON data. Now obviously I have tried it in postman and it's working without any issues. But when I am making the same call in Java using Spring's RestTemplate (with exchange method), the JSON response I am getting is incomplete. Basically, it's giving me response like the part which is missing was never there in response. For example,
In Postman, the response looks like this:
{
"key1": object 1,
"key2": object 2
}
But in Java, the response looks like this:
{
"key1": object1
}
The response is incomplete. Also, after doing some analysis I have found that there is this response header: content-length, it's value in Postman is 933 and in Java it's 840. What can be done to solve this problem?
I am trying to build a rest api that should consume json/x-application data. Now I have looked into two libraries javax.json-api and org.json for handling the data.
Example JSON:
{
"error": "false",
"error_msg": "",
"version": "1.13.10",
"result": {
"malware": {
"finding1": {
"file": "/path/to/filep",
"malware": "{HEX}r2h.malware.blue.44"
}
}
},
"newest_version": "1.13.10"
}
If I now consume this with javax JsonObject, it will work and I can go on with my code. BUT, if i instead post this data and I use org.json.JSONObject I will receive response at the client:
Unrecognized field "error" (class org.json.JSONObject), not marked as ignorable
Tried to find responses on the web, but I didnt step over anything that explains this?
Regards and Thanks
Well,
I do not really know if there is a solution for this. Eventually the REST architectural style does not support JSONObject (org.json.JSONObject). However, the workaround is pretty easy, just consume the json as a String (still you can declare HTTP Request to enforce the type application/json).
So this could look like the following:
#Path("/myendpoint")
#POST
#Consumes(MediaType.APPLICATION_JSON)
public String receiveRequest(String json) {
JSONObject jo = new JSONObject(json);
...
}
First off all your json is valid json. Second thing you are getting a error in response, it means error is not in your code. And third thing the error is "Unrecognized field" it means the POJO class inside the client code does not contain field "error".
I have to construct Orchestration API which calls internally GET API and the response body of GET call should be passed has the request body of POST API automatically using java Spring boot
first :-> get call: /students/{id}/information and response will be
{
"id": "123",
"name": "abc",
"marks": 80
}
Now the above output of get response should be passed as request body for post call. instead of doing manual copying get response and pasting as request body to post call.
it should be done automatically
please post your idea's
thanks
I have to create a couple of web services for validating possible values of given fields. I'm contemplating having something like:
POST /entity/fieldName body { fieldValues }
where the POST will return 400 (Bad request) if the arguments are invalid and 422 (Unprocessable entity) otherwise. However I do not really like the 422 response part very much since it makes the request always return an error. On the other hand since I'm only doing validation and this is a POST I don't want to actually create a new resource on the server (i.e. return 200). Is there another HTTP method / API endpoint that is better suit for this? For what it's worth I will be checking that the entity field with <fieldName> has its value in a given range.
If all you do is validating, then I think you should send 422 by validation error and 200 by validation success. The POST does not mean you have to always create a new entity.
The action performed by the POST method might not result in a
resource that can be identified by a URI. In this case, either 200
(OK) or 204 (No Content) is the appropriate response status, depending
on whether or not the response includes an entity that describes the
result.
If a resource has been created on the origin server, the response
SHOULD be 201 (Created) and contain an entity which describes the
status of the request and refers to the new resource, and a Location
header (see section 14.30).
https://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html
I'm prefer google's api error response style.
So my service sends error response as json or xml and 400 Bad request error code:
{
"status": "INVALID_REQUEST",
"type": "ERROR_MSG",
"data": {
"request": "/v2/data?age=23d",
"errors": [
"age: not a number"
]
},
"time": -1
}
otherwise 200 and corresponding message
I want to be able to generically handle a mass of commands as a single POST request represented as a map of URL => Body, that processes each command via its matching RequestMapping and returns a map of URL => Response.
Request:
{
"/api/things/34?huh=wat": {
"method": "GET"
},
"/api/dogs": {
"method": "POST",
"body": /* some dog-esque json */
}
}
Response:
{
"/api/things/34?huh=wat": {
"response": /* thing-esque json */
},
"/api/dogs": {
"response": /* some error json */
}
}
I am not concerned too much with the shape of the request/response objects, I just don't know how to handle this in Spring.
It sounds like the client should be splitting the individual requests up and sending them one at at a time. Think about it this way, what happens if the POST succeeds but the GET returns a 500 server error? Do you take the status of the whole request to be 500? And what about the POST, does it now need to be rolled back?