I am trying to access CI data from CMDB(BMC atrium) via groovy script and it goes in two steps.
First to send a POST request to login api and get the authorization token.
Second to use this token in subsequent api calls.
I have tested both the APIs in postman and they are working fine.
The part of fetching the token is working fine in groovy script but for the part of fetching CI data using this token is returning error json :
"JsonResponse is[{"messageType":"ERROR","messageText":"A user name must be supplied in the control record","messageAppendedText":null,"messageNumber":149}]"
Here is the funny part, I went back to postman and in the authorization header I just placed a freshly generated token and it gave me the same error JSON as mentioned above. And when I appended "AR-JWT" with the token value in the authorization header - it worked fine!
I have tried passing different values in the header like
- AuthenticationInfoValue.authentication = username , Authorization-Type , username and password etc. but nothing seems to work.
I have checked the official BMC site for documentation but according to it I am doing everything right. If someone can point out what I am doing wrong, it would be really helping.
here is the groovy script
@Grab(group='org.codehaus.groovy.modules.http-builder', module='http-builder', version='0.7')
//post call to get the token
def token
def httpRequester = new
groovyx.net.http.HTTPBuilder("http://<localhost>:<port>/api/jwt/login");
httpRequester.request(groovyx.net.http.Method.POST) { req ->
requestContentType = groovyx.net.http.ContentType.URLENC
body = [username:'user', password:'pass']
response.success = { resp , reader ->
println "response success"
httpResponse = resp
println "httpResponse : "+httpResponse
token = reader.getText()
println "token : "+token
}
response.failure = { resp , reader ->
println "response failure"
httpResponse = resp
println "httpResponse : "+httpResponse
token = reader.getText()
println "token : "+token
}
}
//Get call to fetch the CI Data
httpRequester = new groovyx.net.http.HTTPBuilder("http://<localhost>:<port>/api/arsys/v1/entry/AST:ComputerSystem")
def finalToken = "AR-JWT" + " " + token ;
httpRequester.request(groovyx.net.http.Method.GET) { req ->
println "Inside request"
def headerMap = ['Content-Type': 'application/x-www-form-urlencoded' ,
'Authorization': finalToken]
httpRequester.setHeaders(headerMap)
println "headers : "+httpRequester.getHeaders()
requestContentType = groovyx.net.http.ContentType.URLENC
response.success = { resp, json ->
println "response success"
httpResponse = resp
jsonResponse = json
}
response.failure = { resp, json ->
println "response failure"
httpResponse = resp
jsonResponse = json
}
}
jsonResponse = new groovy.json.JsonBuilder(jsonResponse);
println "JsonResponse is" +jsonResponse.toString();
I found the problem after alot of debugging and it turns out it was quite silly.
In GET API call had to set headers like below.
And Comment out or remove below lines.