In my Jenkins Shared Library, I have implemented the following function:
/* This function lists all the groups */
def list_groups(server_url, each_group_name, authentication){
def groups_url = server_url + "/api/v1/groups"
def response = httpRequest consoleLogResponseBody: true,
contentType: 'APPLICATION_JSON',
customHeaders: [[maskValue: false, name: 'Authorization', value: authentication]],
httpMode: 'GET', ignoreSslErrors: true, responseHandle: 'NONE', url: groups_url,
validResponseCodes: '100:599'
if(response.status == 404){
throw new Exception("The server URL was not found! Please provide a correct server URL.")
}
else{
if(response.status == 400 || response.status == 403){
throw new Exception("Invalid Access token or Access token expired!")
}
}
def result = readJSON text: """${response.content}"""
}
=====================================================================
The response I am receiving is:
Response Code: HTTP/1.1 200 OK
Response:
[{"id":2,"name":"Default User"},{"id":3,"name":"fos"},{"id":4,"name": "kXR"},{"id":5,"name": "Sgh"},{"id":6,"name":"ksn"},{"id":7,"name":"ALb"}]
Success: Status code 200 falls within the accepted range: 100-599
Requirement:
I need to extract and store the last output from the JSON body (id & name) ---> {"id":7,"name":"ALb"} from the response using groovy.