如何通过从Soapui Groovy中其他请求的响应中获取值来设置JSON POST请求中的值



我是新手的脚本脚本,已经在Groovy脚本中获得了请求,并获得了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,则可以使用一个库,例如https://github.com/jwagenleitner/groovy-wslite来构建并发送下一个休息请求。

但是,为什么不使用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)

最新更新