Forum Discussion

tamnor's avatar
tamnor
Occasional Contributor
6 years ago

Extracting value from requestBody and using it in mockResponse

Hi, 

I am using Service V to create a mock response and I want to extract data from the json request body and use it in the response.

 

The following is the json request body ..

 

{
  "employName": "Pat McGraff",
  "companyId": "80966",
  "contactDetails": {
    "emailAddress": "patmcgraff@gmail.com",
    "phoneNumber": "8427306667"
  }
}

 

I basically want to just use this same block in my mock response with additional data e.g. the response body would be 

{
  "employName": "Pat McGraff",
  "companyId": "80966",
  "contactDetails": {
    "emailAddress": "patmcgraff@gmail.com",
    "phoneNumber": "8427306667"
  }
},
  "company": "company123",
  "location": "India",
  "client": "client123",
  "team": "BIG"
}

Is there a way I can do this using the context and requestContext in Service V in the scripting dispatch method?

 

Many thanks.

 

 

 

1 Reply

  • AlexKaras's avatar
    AlexKaras
    Champion Level 3

    Hi,

     

    Yes, this seems to be possible.

    Using this reference (https://community.smartbear.com/t5/SoapUI-Open-Source/Extracting-child-node-from-JSON-Response/m-p/154308) I entered the following code in the Script field of default response and got the expected result in the mock response.

    import groovy.json.JsonSlurper
    import groovy.json.JsonOutput
    
    def payload = mockRequest.requestContent
    //log.info(payload.toString())
    
    def json = new JsonSlurper().parseText(payload.toString())
    //log.info(json)
    
    def myData = [
    "company": "company123",
    "location": "India",
    "client": "client123",
    "team": "BIG"
    ]
    
    json << myData
    //log.info(json.toString())
    
    mockResponse.responseContent = new JsonOutput().toJson(json)