SOAP UI-为同一REST API端点的两个不同的POST请求有效负载返回两个不同的响应 [英] SOAP UI - Return two different responses for two different POST Request Payloads for same REST API end point

查看:50
本文介绍了SOAP UI-为同一REST API端点的两个不同的POST请求有效负载返回两个不同的响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个REST POST API端点-"abc/def".
它的请求有效负载具有(在许多其他字段中)一个字段"yourId",该字段可以采用1或2,如下所示:

I have a REST POST API end point - "abc/def".
It's request payload has (out of many other fields) a field "yourId" which can take either 1 or 2 as shown below:

{
  "yourId":"1"
}

OR

{
  "yourId":"2
}

基于"yourId"的值,我需要返回两个不同的响应:1. YOUR_RESPONSE_1或2. YOUR_RESPONSE_2,我为此编写了一个groovy脚本,如下所示:

On the basis of the value of "yourId", I need to return two different responses either 1. YOUR_RESPONSE_1 OR 2. YOUR_RESPONSE_2 for which I have written a groovy script as shown below:

def requestBody = mockRequest.getRequestContent()
log.info "Request body: " + requestBody
yourId="yourId"
id1="1"
id2="2"
if(requestBody.contains(yourId+":"+id1)){
    return "YOUR_RESPONSE_1"
}else if(requestBody.contains(yourId+":"+id2)){
    return "YOUR_RESPONSE_2"
}else return "ERROR_RESPONSE" 

当我从邮递员到达终点"localhost:8080/abc/def"时,我得到了ERROR_RESPONSE.我该如何解决.

When I hit the end point "localhost:8080/abc/def" from postman, I get ERROR_RESPONSE. How can I fix it.

推荐答案

我建议您使用JSONSlurper(),因为这样可以避免使用转义字符并使脚本清晰易读,而且在输入JSON时也很方便很复杂

I would suggest you to use the JSONSlurper() as this avoids the use of escape characters and makes the script legible, Also it come in handy when the input JSON is complex

def requestBody = mockRequest.getRequestContent()
def parsedJson = new groovy.json.JsonSlurper().parseText(requestBody)
def ID =  parsedJson.yourId

if(ID=="1"){
    return "YOUR_RESPONSE_1"
}
else if(ID=="2"){
    return "YOUR_RESPONSE_2"
}
else return "ERROR_RESPONSE" 

这篇关于SOAP UI-为同一REST API端点的两个不同的POST请求有效负载返回两个不同的响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆