我有一个SoapUI REST(即非SOAP(模拟服务,它返回POST请求的响应。 请求和响应都包含 JSON 内容。
目前,我可以让它返回静态响应并且工作正常,但我希望响应中的某些值动态来源于请求。
因此,如果我有此请求:
{
"the_request":{
"abc":"123",
}
如何在响应中复制"abc"的值?
调查使我相信我可以通过在响应中包含变量来做到这一点,例如:
响应:
{
"the_response":{
"value_from_request":"${#MockResponse#Request#the_request#abc}",
"other":"stuff",
}
然后通过"脚本"选项卡实现脚本以填充响应中的变量。 然后,如何使用请求中的数据填充它?
目前 SoapUI 只生成一个空值"value_from_request":"",
已尝试在"脚本"选项卡中使用 mockRequest.requestContent,但尚未找到如何从中获取"123"值。
好的,解决了这个问题。因此,响应消息可以简单地引用requestContext中的变量,如下所示:
响应:
{
"the_response":{
"value_from_request":"${the_value}",
"other":"stuff",
}
并且可以使用时髦的脚本来解析JSON请求内容,并在requestContext中填充"the_value"或任何你喜欢的内容:
// Parse the JSON request.
def requestBody = new groovy.json.JsonSlurper().parseText(mockRequest.getRequestContent())
// Set up "the_value" from the request message.
requestContext.the_value = requestBody.the_request.abc
// Bit of logging so can see this in the "script log" tab.
log.info "Value extracted from request: ${requestContext.the_value}"
我认为脚本应该是这样的
def requestBody = new groovy.json.JsonSlurper().parseText(mockRequest.getRequestContent())
context.setProperty("the_value",requestBody.the_request.abc)