Orchestration API - (POST And Get API) using spring boot - java

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

Related

How can we perform 2 get request at same time Rest assured API Testing

JAVA RESTASSURED APITESTNG CUCUMBER
when I send get request using path variable and query param, I get a token in the response body and I will not get proper response until I add that token as a query param and send another get request.
So How can I perform that?
Given Cucumber Feature File
Given API has the following filed <"fieldName">
When API sends a "GET" request to "TranscationAPI"
Then API will receive the response code as 200
And the body will have following field
Should I change it to something like this?
Given API has the following filed <"fieldName">
When API sends a "GET" request to "TranscationAPI"
Then API will receive token as ""
When API sends again "GET" request to "TranscationAPI"
Then API will receive the response code as 200
And the body will have following field
Or I can use handle this through a different method.
Background is the great place to obtain token - see https://cucumber.io/docs/gherkin/reference/#background
It's possible by Java multithreading mechanism - https://www.tutorialspoint.com/java/java_multithreading.htm

RestTemplate GET call gives incomplete response

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?

Using Spring MVC and Kotlin, why does this Body is ignored?

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.

Consume web service which produces stream (streams some binary data as output)

I have a REST Java service which returns a file as a stream. It's similar to the output approaches described here:
Input and Output binary streams using JERSEY?
I also use StreamingOuput as described above.
I now wonder now how to call this method on the client side. I use Swagger but I am not very good with it, and I have no idea how to call my method.
Currently in the web service I have defined
#ApiOperation(value = "Export file from the database", response = File.class)
but I doubt this File.class is a good idea here.
Do I need to modify this response type? What is a good way of calling my service?
When I call my service from the Swagger UI interface I see these response headers.
{
"date": "Sun, 27 Aug 2017 11:14:20 GMT",
"content-disposition": "attachment; filename=\"DealerTexts.xlsx\"",
"x-powered-by": "Servlet/3.1 JSP/2.3 ( Java/Oracle Corporation/1.8)",
"transfer-encoding": "chunked",
"server": "",
"content-type": "application/octet-stream"
}
In the response body it gives me a link which I can click and download the file generated by the web service. So my question is just how I can call this same thing from Java code on the client side.

REST api for field validation

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

Categories