Forum Discussion
Mike_Rodrigues
5 years agoProduct Manager
@Vitor Santos, looks like their API uses Basic Auth, so it should be fairly trivial to make simple calls with our built-in HTTP library, and parse the JSON with JSON Slurper:
import com.santaba.agent.groovyapi.http.HTTP
import groovy.json.JsonSlurper
hostname = hostProps.get("system.hostname")
port = hostProps.get("api.port")
user = hostProps.get("api.user")
pass = hostProps.get("api.pass")
httpClient = HTTP.open(hostname, 443, true)
httpClient.setAuthentication(user, pass)
/*
* This helper function handles any HTTP GETs.
*/
def httpGET(endpoint) {
httpClient.get(endpoint)
if (httpClient.getStatusCode() == 200) {
return new JsonSlurper().parseText(httpClient.getResponseBody())
} else {
println "STATUS CODE:\n${httpClient.getStatusCode()}\n\nRESPONSE:\n${httpClient.getResponseBody()}"
}
}
/////// Your Code:
def alarms = httpGET("/alarms")
println(alarms.json_property_you_want_to_access)
return 0;
Basically, you use HTTP.open to connect, setup the auth. We define a helper function to make GET requests and pass the body to JsonSlurper so you end up with a parsed object you can iterate over and print from.
Related Content
- 5 years ago
- 7 months ago
- 8 months ago