如何使用放心在正文中为 post 请求发送 JsonObject?
How to send JsonObject in body for post request using rest assured?
我有一个使用 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");
请指导我。
尝试使用以下代码将 Json 发送到使用 Rest-assured
的 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");
我有一个使用 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");
请指导我。
尝试使用以下代码将 Json 发送到使用 Rest-assured
的 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");