首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >在Java和RestAsured中使用嵌套标签测试post方法时面临的问题

在Java和RestAsured中使用嵌套标签测试post方法时面临的问题
EN

Stack Overflow用户
提问于 2018-06-08 03:36:42
回答 1查看 35关注 0票数 0
代码语言:javascript
复制
RequestSpecification request = RestAssured.given().header("Content-Type", "application/json");
    JSONObject requestParams = new JSONObject();
    JSONObject childObject1= new JSONObject();
    JSONObject childObject2 = new JSONObject();
    requestParams.put("contactClass", "ZWSS"); 
    requestParams.put("contactActivity", "0039");
    requestParams.put("contractAccountNumber", "210024144291");
    requestParams.put("text", "Please rate the overall ease of using the website to initiate or make your service request");
    requestParams.put("contactType", "Z1");
    requestParams.put("contactDirection", "1");
    childObject1.put("question", "0001");
    childObject1.put("answer", "01");
    childObject1.put("question", "0002");
    childObject1.put("answer", "02");

    requestParams.put("addInfo", childObject1);
    requestParams.put("addInfo", childObject2);

    request.body(requestParams.toString());
    Response response = request.post("https://api-dev.adp.com/api/*/*/*");

我正在尝试使用Restassured框架中的嵌套标记来测试Post方法,上面是我在JSON对象中构建请求的代码片段。由于我的JSON没有同时包含Addinfo和request的值(问题和答案),导致请求失败,请求的格式如下。

代码语言:javascript
复制
{"contractAccountNumber":"210024144291",
"contactClass":"ZWSS",
"contactActivity":"0039",
"contactType":"Z1",
"addInfo":{"question":"0002","answer":"02"},
"text":"Please rate the overall ease of using the website to initiate or 
make your service request",
"contactDirection":"1"}

但请求应采用以下格式

代码语言:javascript
复制
 {
"contactClass": "ZWSS",
"contactActivity": "0039",
"contractAccountNumber": "210024144291",
"text": "Please rate the overall ease of using the website to initiate or 
make your service request",
"contactType": "Z1",
"contactDirection": "1",
"addInfo": [{
"question": "0001",
"answer": "01"
},
{
"question": "0002",
"answer": "02"
}
]
}

任何人都可以帮我解决这个问题..

EN

回答 1

Stack Overflow用户

发布于 2018-06-08 03:48:51

您应该将addInfo作为数组传递

代码语言:javascript
复制
    childObject1.put("question", "0001");
    childObject1.put("answer", "01");

    childObject2.put("question", "0002");
    childObject2.put("answer", "02");

    JSONArray jsonArray = new JSONArray();

    jsonArray.put(childObject1);
    jsonArray.put(childObject2);

    requestParams.put("addInfo", jsonArray);
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50749002

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档