Forum Discussion

rajs2020's avatar
rajs2020
Frequent Contributor
4 years ago

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-request-and-response-from-a/m-p/212769. 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.



    • rajs2020's avatar
      rajs2020
      Frequent Contributor

      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()
      }

       

  • aaronpliu's avatar
    aaronpliu
    Frequent Contributor

    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

    • rajs2020's avatar
      rajs2020
      Frequent Contributor

      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)
      • aaronpliu's avatar
        aaronpliu
        Frequent Contributor

        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

  • nmrao's avatar
    nmrao
    Champion Level 3
    Did you get chance to look at all the replies? or you only do selective replies?