'How to send a get request and retrieve response data in groovy?
I have a REST Api and a groovy script from which in want to send a get request to my api and retrieve the response in an object.
the api returns a list of values like this :
[
200,
200,
200,
200,
200,
200,
200,
200,
200,
200,
200,
200
]
i want to send a GET request and retrieve this response in a variable in the most efficient and simple way
Solution 1:[1]
You could do somehting like this:
import wslite.rest.RESTClient
class App {
static void main(String[] args) {
RESTClient client = new RESTClient("http://localhost:8080/")
def path = "/demo"
def response = client.get(path: path)
List listOfNumbers = response.json
println listOfNumbers
}
}
That RESTClient is provided by groovy-wslite. In Gradle you could express that dependency with something like this:
implementation 'com.github.groovy-wslite:groovy-wslite:1.1.3'
Alternatively you could use a standalone script like this:
@Grab("com.github.groovy-wslite:groovy-wslite:1.1.3")
import wslite.rest.RESTClient
RESTClient client = new RESTClient("http://localhost:8080/")
def path = "/demo"
def response = client.get(path: path)
List listOfNumbers = response.json
println listOfNumbers
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Jeff Scott Brown |
