Is there an easy way to access parts of an API response in a groovy script?
SOLVED- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Is there an easy way to access parts of an API response in a groovy script?
This is a follow up to my old question - https://community.smartbear.com/t5/API-Functional-Security-Testing/ReadyAPI-How-to-access-the-entire.... In a groovy script, I would like to be able to access the entire response of an API. For example, like this:
def response = context.expand('Something')
def content_type = response.headers(''content-type")
def name = response.body(jsonPath : students[5th].name)
Is there an easy way to break up a response object into parts like http code, headers, body etc and then extract values from each of these parts? I prefer not to use long and ugly xml like Test#ResponseAsXml/Some long ugly path.
Solved! Go to Solution.
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Have a look at the script, hope it helps. follow the comments in the script.
https://github.com/nmrao/soapUIGroovyScripts/blob/master/groovy/AccessResponseData.groovy
Regards,
Rao.
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hi @rajs2020 ,
response status:
def teststep = testRunner.testCase.testSteps['Your Test Step']
def responseStatus = teststep.testRequest.response.responseHeaders["#status#"]
response header:
def responseHeader = teststep.testRequest.response.responseHeaders
response body:
def responseBody = teststep.testRequest.response.contentAsString
Thanks,
/Aaron
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
@aaronpliu - Thanks for your reply. Some questions.
def teststep = testRunner.testCase.testSteps['Your Test Step']
def responseStatus = teststep.testRequest.response.responseHeaders["#status#"]
1) Could you please point me to the official documentation on these classes and functions like testRunner etc.?
2) Is there a table of keywords to use for other headers besides "#status#"?
3) Like the headers, can I easily access body also, maybe like this -
teststep.testRequest.response.body(Json path query to get a field)
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Regards,
Rao.
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hi @rajs2020
Answer your questions:
A1: Go to smartbear official online document to look for API docs of ReadyAPI (SoapUI)
A2: responseHeaders return a map object, you can output this object and to see which info would be access
A3: Since you already get response object, can directly get response body (e.g.
def responseBody = teststep.testRequest.response.contentAsString
if you get response body, then using JsonSluper (if it is json response) to parse it, you can retrieve any node / value under json object.
For example:
json data: {“a": {"b": [{"c": 1, "d": 2}]}}
import groovy.json.JsonSlurper
def json = new JsonSlurper().parseText(responseBody)
// output b
log.info json.a.b
// output value of b
for (def x in json.a.b) {
log.info x
}
Thanks,
/Aaron
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
@nmrao - Thanks, I saw your solution also now. It works, but I have some questions.
1 - Why do you use closures here instead of regular functions?
2 - How do we reuse the code in other suites also instead of repeating the code?
PS - I modified your solution as follows.
/*
Sample response:
Content-Type: application/json; charset=utf-8
{"healthy": true}
*/
//The test step which calls the API.
def stepName = 'CallApi'
assert getHeader(stepName, 'Content-Type') == 'application/json; charset=utf-8'
assert getJson(stepName).healthy == true
//Utility functions:
def getStepResponse(stepName){
context.testCase.testSteps[stepName].httpRequest.response
}
def getJson(stepName){
new groovy.json.JsonSlurper().parseText(getStepResponse(stepName).responseContent)
}
def getHeader(stepName, header){
getStepResponse(stepName).responseHeaders[header].first()
}
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
1. Refer:
https://stackoverflow.com/questions/21905550/groovy-closures-vs-methods-the-difference
2. Use Script Library for reusing. Refer:
https://www.soapui.org/docs/scripting-and-properties/scripting-and-the-script-library/
Regards,
Rao.
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
@nmrao - Thanks. I was not asking about closures vs functions in general. I was asking why you used closures in your code. If there are reasons for it, then I can do the same in my code.
PS -
As an aside, here is the ReadyAPI docs for Groovy Script Library https://support.smartbear.com/readyapi/docs/testing/scripts/libs/groovy-lib.html
