我有一个使用 Google Gson 创建的 JsonObject。
JsonObject jsonObj = gson.fromJson(response1_json, JsonElement.class).getAsJsonObject();
我还对现有的 jsonobj 做了一些修改,如下所示:
JsonObject newObject = new JsonObject();
newObject.addProperty("age", "50");
newObject.addProperty("name", "X");
jsonObj.get("data").getAsJsonArray().add(newObject);
现在,使用放心,我需要使用这个 jsonobject 发送 post 请求。我尝试了以下方法,但它不起作用并引发异常:
Response postResponse =
given()
.cookie(apiTestSessionID)
.header("Content-Type", "application/json")
.body(jsonObj.getAsString())
.when()
.post("/post/Config");
请指导我。
尝试下面的代码使用 Rest-assured 将 Json 发送到 Post 请求
//Get jsonObject from response
JsonObject jsonObj = gson.fromJson(response1_json, JsonElement.class).getAsJsonObject();
//Create new jsonObject and add some properties
JsonObject newObject = new JsonObject();
newObject.addProperty("age", "50");
newObject.addProperty("name", "X");
//Get jsonarray from jsonObject
JsonArray jArr = jsonObj.get("data").getAsJsonArray();
//Add new Object to array
jArr.add(newObject);
//Update new array back to jsonObject
jsonObj.add("data", jArr);
Response postResponse =
given()
.cookie(apiTestSessionID)
.contentType("application/json")
.body(jsonObj.toString())
.when()
.post("/post/Config");
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)