【问题标题】:How to post nested json in restassured如何在放心中发布嵌套的json
【发布时间】:2020-12-14 10:18:02
【问题描述】:

下面是我正在使用的代码

JSONObject requestParams = new JSONObject();
requestParams.put("something", "something value");
requestParams.put("another.child", "child value");

这就是需要发布api的方式

{

   "something":"something value",
   "another": {
   "child": "child value"
   }
}

我收到一条错误消息,指出

another.child 字段是必需的。

如何通过 restAssured 发布此内容?其他 API 不需要发布嵌套工作,所以我假设这就是它失败的原因。

【问题讨论】:

    标签: rest-assured rest-assured-jsonpath


    【解决方案1】:

    将 another 视为主要 Json 对象中的 Json 对象。

    JSONObject requestParams = new JSONObject();
    requestParams.put("something", "something value");
    
    JSONObject anotherObject = new JSONObject();
    anotherObject.put("child", "child value");
    
    requestParams.put("another", anotherObject);
    
    System.out.println(requestParams.toString());
    

    这将打印出来;

    {"another":{"child":"child value"},"something":"something value"}
    

    将此 Json 对象传递给 Rest Assured POST 方法;

        RestAssured.baseURI="base_url";
    
        RestAssured.given()
                .when()
                .post(requestParams.toString());
    

    必需的进口;

    import org.json.JSONObject;
    import io.restassured.RestAssured;
    

    【讨论】:

    • 谢谢。我不得不发送类似 changeDetails" : { "requirement" : "jira" } 所以,我使用了 changeDetailsObject.put("requirement", "jira"); jsonPayloadBody.put("changeDetails", changeDetailsObject); .我赞成这个答案和问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-07
    • 1970-01-01
    • 1970-01-01
    • 2017-11-13
    • 1970-01-01
    相关资源
    最近更新 更多