如何将REST POST响应中的JSON值传输到SOAPUI中的REST Get请求

时间:2015-10-20 16:10:13

标签: json rest soapui

我有一个REST服务,我正在使用SoapUI进行测试。我的TestSuite的第一步返回以下Response(Json):

{   
  "mtMessageId": 52003685,   
  "status":    
  {
     "value": 0,
     "code": "OK",
     "text": "Text message submitted"   
  },   
  "custMessageId": 123,   
  "custMessageRef": null
}

我想转移'在下一步中将mtMessageId中的值转换为HTTP Get Request。

请求的格式类似于" / SMS / {id}"

如何将值转移到请求中?

1 个答案:

答案 0 :(得分:1)

首先,您必须使用属性(例如使用get)在您的请求中设置/SMS/${#TestCase#id}方法的资源,以便从第一个请求中检索它。

enter image description here

然后在您的请求之间添加groovy script testStep。使用以下代码从第一个请求的json响应中获取id,并将其设置为第二个请求的属性。

import groovy.json.*

// get the response from the first request using its name
def response = context.expand('${Request 1#Response}')
// parse it
def json = new JsonSlurper().parseText(response)
log.info json.mtMessageId
// get the json value an set it as property in testCase
context.testCase.setPropertyValue("id",json.mtMessageId.toString())

请注意,您可以使用属性传输testStep从请求中获取值并将其设置为属性,但是由于SOAPUI将all转换为xml,我更喜欢使用groovy script来工作json。

希望它有所帮助,