如何在SOAPUIGroovy中通过从其他请求的响应中获取值来设置JSON post请求中的值

如何在SOAPUIGroovy中通过从其他请求的响应中获取值来设置JSON post请求中的值,groovy,soapui,Groovy,Soapui,我是Groovy脚本新手,在Groovy脚本中完成了get请求,并获得了JSON响应,如下所示: { Id: 12, Cntid: 3, MrId: 1257 Details: [{ stid: 224 trqty: 2, Soh: 22 }, { stid: 224, trqty: 2, Soh: 27

我是Groovy脚本新手,在Groovy脚本中完成了get请求,并获得了JSON响应,如下所示:

{
    Id: 12,
    Cntid: 3,
    MrId: 1257
    Details: [{
            stid: 224
            trqty: 2,
            Soh: 22
        }, {
            stid: 224,
            trqty: 2,
            Soh: 27
        }, {
            stid: 2341,
            trqty: 21,
            Soh: 89
        }
    ]
}
从中检索数据并创建操作后请求,如下所示:

{
    Id: 12,
    Cntid: 3
    Details: [{
            stid: 224
            trqty: 2
        }, {
            stid: 224,
            trqty: 2
        }, {
            stid: 2341,
            trqty: 21
        }
    ]
}

这有助于从有效的JSON开始:您的负载中缺少一些逗号

假设您只想从响应中删除几个属性,您可以使用:

import groovy.json.JsonSlurper

// Get the existing response
def newRequest = new groovy.json.JsonSlurper().parseText( [your JSON string] )

// Remove the unwanted attributes
newRequest.remove('MrId')
newRequest.'Details'.each {
    it.remove('Soh')
}

// Add new attributes
newRequest.put('status' , 'oma');
newRequest.'Details'.each {
    it.put('active','y')
}
如果您想持久使用Groovy,可以使用库(如)来构造并发送下一个REST请求

但是,为什么不使用soapUI来实现这一点:

// Get the next test step in a test case and set the request
def secondREST = context.testCase.getTestStepByName("secondREST")
secondREST.httpRequest.requestContent = newRequest

// Run it
secondREST.run(testRunner, context)

如果您使用的是soapUI,为什么要在Groovy脚本中使用get和post?这是在soapUI测试用例的上下文中完成的吗?我需要自动化脚本,每次运行请求时,响应都会有所不同,因为post请求也会发生变化。您仍然可以对这两个请求使用soapUI测试步骤。运行初始请求后,使用Groovy修改响应,并在下一个测试步骤中将更改后的负载设置为请求。您的第二个请求最初可能有一个空的请求负载,因为下面的Groovy代码将替换它。否则,只需使用回答中提到的groovy wslite库来执行您自己的调用即可。那么,在将请求作为请求发送之前,我将如何在post请求负载中添加额外的值?就像我想添加状态:“oma”和详细信息一样,我想添加“活动”:“y”代码在上面更新
JsonSlurper
将文本解析为列表和地图的数据结构,以便使用标准方法添加和删除属性。试一试。