cancel
Showing results for 
Search instead for 
Did you mean: 

Extracting value from requestBody and using it in mockResponse

Occasional Contributor

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 1
Community Hero

Re: Extracting value from requestBody and using it in mockResponse

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/1...) 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)

 

Regards,
Alex
[Community Expert Group]
____
[Community Expert Group] members are not employed by SmartBear Software but
are just volunteers who have some experience with the tools by SmartBear Software
and a desire to help others. Postings made by [Community Expert Group] members
may differ from the official policies of SmartBear Software and should be treated
as the own private opinion of their authors and under no circumstances as an
official answer from SmartBear Software.
[Community Expert Group] signature is used with permission by SmartBear Software.
http://smartbear.com/forums/f83/t86934/community-experts/
================================
New Here?
Join us and watch the welcome video: