首页 文章

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

提问于
浏览
0

我有一个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 /

如何将值传输到请求中?

1 回答

  • 1

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

    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 .

    希望能帮助到你,

相关问题